Click here to Skip to main content
15,796,299 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have defined a base class IErrorLog and derived class FileErrorLog.
C++
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");


How can ptr access private function of FileErrorLog class?
Posted
Comments
Philippe Mori 14-Sep-15 13:01pm    
Usually, you don't make a function less accessible in a derived class.

ptr is declared as poiner to IErrorLog, hence it obeys access rules defined in IErroLog class. Please note, access is checked at compile time while dynamic binding happens at runtime.
 
Share this answer
 
v2
Comments
the_beginner 14-Sep-15 4:44am    
thanks
CPallini 14-Sep-15 4:57am    
You are welcome.
The whole purpose of private members is to prevent any access to them from anywhere except the functions of the same class.

Please see my post: http://www.codeproject.com/Members/SAKryukov?msg=5069181#xx5069181xx[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900