Question

when i declare a protected data member in a class that means its not accesible to the outer world but the derived class. My question is

will it be accesible to a class that is derived from the derived class?

Was it helpful?

Solution

Yes, protected data members are accessible all the way down the inheritance hierarchy.

Protected data is usually better avoided. An alternative is to write protected methods that access the private data. This keeps the data encapsulated within a single class. It also makes it easy to set a breakpoint for changes to the data.

OTHER TIPS

Actually, a protected data member is accessible only using protected and public inheritance.

Here is an example where a protected member is not accessible in the derived class:

class Base  
{  
protected:  
int iBase;  
};

class Derived : private Base  
{  
// iBase not accessible  
};

Yes. (You could have simply tried that out, by the way.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top