More and more I'm finding myself snagged on very simple problems...
Anyway, I have two classes - we'll say top and bottom, such that bottom is a derived class from top. Top contains private and public members, and bottom
is supposed to have free access to them. I need to be able to freely use class polymorphism (which I've never had problems with
before...) and while using such polymorphism, continue to access members from Top. Here's what I have looks like:
class top{
public:
//some members
protected:
top* parent;//important later
int foo;//later
//more members
};
class bottom : public top{
public:
void bar(void);//again, later
//some others
private:
//not many
};
This, so far, is a no brainer. The problem is I'm getting an access violation error in bottom::bar when using parent->foo.
void bottom::bar(){
int n = foo+parent->foo;//error C2248: 'top::foo' : cannot access protected member declared in class 'top'
}
The exact reason for this construction is this: I'm writing a simple GUI class, and certain types of windows need to refer directly to their parent's value (namely "child" windows like buttons) in order reposition themselves to be at the parents location + offset. They don't, however, know what their parent is except that it is a derived class from the GUI base class.
So, for instance, a menu can have buttons, but then so can a text box.
I'd appreciate any explanation as to what I'm doing wrong... I feel I have significant holes in my C++ knowledge, probably because I had to teach myself from google =/