private members driving me crazy

rifter1818

Well-known member
Joined
Sep 11, 2003
Messages
255
Location
A cold dark place
my problem is this, i have a class (we shall name A) inheriting class B and my compiler will not let my A::[memberfunction] access the private variables in class B, which of course through inheritance are now variables in class A. this is driving me nuts, it can be ignored in this case because its in a dll, so i can make everything in B public and just not include the should be private ones in the including header file. but shouldnt a class that inherits something be able to access its own members?
 
Last edited by a moderator:
In order for a derived type to have access to its base members, you have to put it in protected scope, private members cant be accessed.
Code:
class B
{
private:
    int privateInt;     // A cannot access
protected: 
    int protectedInt; // A can access
};

class A : public B
{
public:
    void SetNumber(int value) { protectedInt = value; }
    int GetNumber() { return protectedInt; }
};
 
That is kind of the point of private members, if they need to be accessed in sub classes make them protected or provide protected accessors to them.
 
Last edited by a moderator:
Back
Top