本文介绍了指向派生对象的基指针如何访问其私有成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经定义了一个基类IErrorLog和派生类FileErrorLog。
I have defined a base class IErrorLog and derived class FileErrorLog.
class IErrorLog
{
public:
virtual bool OpenLog(const char *strFilename) = 0;
virtual bool CloseLog() = 0;
virtual bool WriteError(const char *strErrorMessage) = 0;
};
class FileErrorLog : public IErrorLog
{
bool OpenLog(const char *strFilename)
{
cout << strFilename <<" is opened in FileErrorLog"<<endl;
return true;
}
bool CloseLog()
{
cout << "Close Log in FileErrorLog"<<endl;
return true;
}
bool WriteError(const char *strErrorMessage)
{
cout << "Error: "<<strErrorMessage<<" in in FileErrorLog"<<endl;
return true;
}
public:
FileErrorLog(){}
};
int main()
{
FileErrorLog *f1= new FileErrorLog;
IErrorLog *ptr = new FileErrorLog;
bool er=ptr->WriteError("Memory finished");
bool f=f1->WriteError("direct");
ptr如何访问FileErrorLog类的私有函数?
How can ptr access private function of FileErrorLog class?
推荐答案
这篇关于指向派生对象的基指针如何访问其私有成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!