Jump to content
  • Advertisement
Sign in to follow this  
Silly_con

Access private base members from derived

This topic is 4880 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!