rifter1818 Posted December 22, 2004 Posted December 22, 2004 (edited) 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? Edited December 22, 2004 by rifter1818 Quote
BlackStone Posted December 22, 2004 Posted December 22, 2004 In order for a derived type to have access to it's base members, you have to put it in protected scope, private members can't be accessed. 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; } }; Quote "For every complex problem, there is a solution that is simple, neat, and wrong." - H. L. Mencken
Administrators PlausiblyDamp Posted December 22, 2004 Administrators Posted December 22, 2004 (edited) 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. Edited December 22, 2004 by PlausiblyDamp Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.