我有这段代码:
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";
}
};
有两个函数throw exception_not_implemented:
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 &)'
如果我删除上面显示的两个 throw,效果很好。但是exception_oob不会发生同样的错误。我不明白为什么。
最佳答案
可以临时添加一个私有(private)的拷贝构造函数声明,在拷贝的时候会报错。然后您可以修复该代码以使其不进行复制。
关于c++ - 奇怪的 C++ 访问私有(private)成员问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20578949/