奇怪的C++访问私人成员问题
Odd C++ accessing private member issue
我有一段代码:
class object
{
public:
virtual ~object(){ }
bool equals(const object& J)const
{
return &J == this;
}
int operator==(const object& J)const
{
return equals(J);
}
virtual int getHash()const;
virtual void getType()const;
void* operator new(size_t size)
{
void*mem = malloc(size);
return mem;
}
};
class notcopyable
{
private:
notcopyable(const notcopyable&){}
notcopyable& operator=(const notcopyable&){}
public:
notcopyable(){}
};
class exception :
public object,public notcopyable
{
private:
public:
virtual ~exception();
virtual const char* info();
};
class exception_not_implemented :
public exception
{
public:
exception_not_implemented()
{
}
virtual const char* info()
{
return "exception_not_implemented: ";
}
};
class exception_oob :public exception
{
public:
exception_oob()
{
}
virtual const char* info()
{
return "Index out of boundary";
}
};
有两个函数抛出exception_not_iimplemented:
void object::getType()const
{
throw exception_not_implemented();
}
int object::getHash()const
{
throw exception_not_implemented();
}
得到这个错误:
error C2248: 'js::notcopyable::notcopyable' : cannot access private member declared in class 'js::notcopyable'
编译器的输出显示:
This diagnostic occurred in the compiler generated function 'js::exception::exception(const js::exception &)'
如果我删除上面显示的两次投掷,效果会很好。但exception_oob不会出现同样的错误。我不明白为什么。
您可以临时添加一个私有复制构造函数声明,它将在进行复制时生成一个错误。然后,您可以将该代码修复为不进行复制。
错误应该发生在调用(私有)复制构造函数的其他地方。
例如:
例外情况a;异常b=a;//错误:无法访问私人成员。。。
相关文章:
- 从不同C++类的成员变量访问成员函数
- 结构成员在访问时被清除
- 好友功能 - 成员无法访问
- C++:成员不可访问-使用友元函数允许一个类修改另一个类的成员数据
- 仅当构造函数具有参数时,C++ 公共成员才能访问
- 在 c++ 中创建其成员可访问的类
- 从静态成员函数访问私有非静态类变量 - C++
- 运算符重载(使用binaray friend函数)类没有成员,并且成员不可访问
- 使用C 中的共享_ptr从公共静态成员函数访问私有构造函数
- 使用静态成员函数访问静态数据成员
- 为什么我们可以从 C++ 中的成员函数访问 non_member 函数
- 操作员问题 - 访问冲突
- 长镜头 -- 可能有一个静态类成员,该类成员具有访问非静态成员的 lambda
- 优化成员变量访问
- 使用'this->'成员变量访问的性能
- 从成员变量访问静态 constexpr 成员,GCC 错误?
- 静态成员函数访问静态私有变量时的链接器错误
- 通过const成员变量访问内联函数
- MSVC声称公共成员无法访问
- 奇怪的C++访问私人成员问题