我有這樣一段代碼:奇C++訪問私有成員問題
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_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 &)'
如果我刪除上面顯示的兩個投擲,它運作良好。但同樣的錯誤不會發生在exception_oob上。我無法弄清楚爲什麼。
你確定錯誤在這裏,而不是在你的程序中的其他地方? – pyon
這段代碼適合我編譯。 –
@EduardoLeón它可能是...因爲行號指向我的代碼中顯示的最後一行。 – babel92