Archived

This topic is now archived and is closed to further replies.

c++ inheritance

This topic is 5750 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

Hi. I''ve been experimenting some with classes, and I have a small problem I''ve been fighting with for 3 days now. Help is very appreciated. =) Basically, I have a class: class a { protected: int iAVar; }; Then I have another class inheriting it.. class b : a { // Stuff, nothing of importance for this problem. }; And then last, and this is where my problem is(either here or in "a".. class c : b { public: void DoStuff() { iAVar = 2; } }; However, this gives me this: error C2247: ''iAVar'' not accessible because ''b'' uses ''private'' to inherit from ''a'' Anyone got any ideas? And yes, this code is faked, but I could give you the real thing if you want. Thanks. Eric

Share this post


Link to post
Share on other sites
unless you do state it explicitly, its assumed its private.

ex.

class a
{
protected:
int i;
};

class b: public a
{
};

class c : private b
{
void do_it()
{
i = 0;
}
};

[edited by - KlePt0 on March 21, 2002 2:14:22 PM]

Share this post


Link to post
Share on other sites
class a
{
protected:
int iAVar;
};


class b : public a
{
// Stuff, nothing of importance for this problem.
};


class c : public b
{
public:
void DoStuff() { iAVar = 2; }
};

that should work

Share this post


Link to post
Share on other sites
what about encapsulation?

why do you have a protected member?

best to make it private and manipulate with protected member functions if you only want derived classes to access it.

Share this post


Link to post
Share on other sites