Access private base members from derived

Started by
31 comments, last by MobileOak 19 years, 1 month ago
Is possible to access private data member of a Base class from a derived class in C++? For example:

class Base { 
private: int i;
};

class Derived : public /* private or protected */ Base {
public:
void func(int n) { i = n; }
};


I get this error compiling it in MSVC 6.0: main.cpp(49) : error C2248: 'i' : cannot access private member declared in class 'Base' see declaration of 'i' Thanks
Advertisement
Use "protected" instead of private.
oh, thanks!
class Base { protected:    int i;};class Derived : public Base {public:    void func(int n) { i = n; }};
Rob Loach [Website] [Projects] [Contact]
Making data members with protected access is most often "the sin of satan" and very contradictive, generally a sign of bad design unless there is real rationale for there use and there truely is such a tight coupling between parent & child types, in most case there isn't so don't fall into bad habbits.
Quote:Original post by snk_kid
Making data members with protected access is most often "the sin of satan" and very contradictive, generally a sign of bad design unless there is real rationale for there use and there truely is such a tight coupling between parent & child types, in most case there isn't so don't fall into bad habbits.


Care to explain in more detail? I don't understand why derived classes shouldn't have access to their base class's member variables.
Encapsulation. The variables are private members of that class, and even though another class is derived from that base, it's still a different class. Private specifies access to only members of that base class. Protected specifies access to derived classes and friend functions. What some, like I at the moment, do is to just use the access functions to get to those variables. The data stays locked away and only the base class and its original functions can directly access that data.

I'm sure someone better at this can explain it more easily than I can.
generally, it's a sign of bad design. Usually, the variables should be accessed through the members the base class provides.

Also, you can access the base class's private fields (Note: this is a sign of even worse design) if you make it a friend, like this(it's been a while since I've used friends, so this may be a little off):
class BaseClass {private:   int x;};class DerivedClass : public BaseClass {   friend BaseClass;};
Remember: You should probably never use this, as it's usually bad design, just FYI.
Quote:Original post by MobileOak
Quote:Original post by snk_kid
Making data members with protected access is most often "the sin of satan" and very contradictive, generally a sign of bad design unless there is real rationale for there use and there truely is such a tight coupling between parent & child types, in most case there isn't so don't fall into bad habbits.


Care to explain in more detail? I don't understand why derived classes shouldn't have access to their base class's member variables.


Because it's an implementation detail that doesn't need to be known, it should be pretty obvious why its called an abstraction, i even high-lighted the main terms coupling and also to enforce state invariants, polymorphic types and type inheritance in general is mainly about extending behaviours/operations not extending implementation, i'm not saying never, i'm say in most cases its abused without any rationale it may aswell be public. I'm not gonna go into any more details, just google it and you will get millions of hits about it.

In theory public virtual member functions considered are bad aswell but that is beyond the scope of this if you want to know more then google up on non-virtual interface idiom.
Here is a hack that lets you use private members in a derived class.

class base{private:int i;};class derived : public base{private:using base::i;};


Though that is so ugly I shouldn't have even mentioned it.

This topic is closed to new replies.

Advertisement