Sign in to follow this  

pointer to object

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

Say I have a mother class and a child class, where the child class has overloaded a function of the mother class:
class Mother
{
    public:
    int foo()
    {
        return 1337;
    }
};
And the child class:
class Child : public Mother //Maybe I made a typo here, don't have my code
 available to check if it's correct, but child is a child class of mother
{
    public:
    int foo() //foo() of Mother is overloaded!
    {
        return 42;
    }
};
Now I do the following:
Child child;
Mother* pointer;

pointer = &child; //is this allowed?
int a = pointer->foo();
What should a be equal to now? 42, or 1337? EDIT: there may be some errors in the code, I don't have my computer with my IDE running now which is why I can't test it.

Share this post


Link to post
Share on other sites
The reason I ask this is because I want to make a "GuiWindow" class for my GUI. Such a window must be able to contain a lot of other gui elements and if I drag the window, all these elements must move as well.

So I want to give the window pointers to all elements it contains. All Gui elements (buttons, checkboxes, etc...) are child classes of the mother class "GuiElement". If I move the window (which has pointers of the type GuiElement*), I would like the move() functions of GuiButton, GuiCheckBox, etc... to be called and NOT the useless move() prototype from their mother class GuiElement. I hope you know what I mean...

So for that reason, I really hope the answer is 42. If it's not, what do I have to do to make sure it is, while still being able to see all different kinds of gui elements as one type of object with a move() function?

Share this post


Link to post
Share on other sites
tentoid is correct. In C++ (as opposed to Java, for example), methods are not polymorphic ("virtual") by default, and you need to mark the ones you want to be with the virtual keyword. So, your code would be:


class Mother
{
public:

// virtual added
virtual int foo()
{
return 1337;
}
};

class Child : public Mother
{
public:

// the virtual keyword is not mandatory in child classes
// but is considered good practice
virtual int foo()
{
return 42;
}
};



Share this post


Link to post
Share on other sites
Your mother class has to know who are her children, otherwise she cannot move them. You have to use "Composite pattern":


class GuiElement
{
public:
virtual void Drag(int x, int y) {/* do nothing */};
}

class Mother: public GuiElement
{
public:
virtual void Drag(int x, int y)
{
/* <- Drag me! -> */
for each pointer pChild in the list of children
pChild->Drag(x,y);
}
private:
/* <- list of pointers to GuiElement -> */
}

class Child(ren) (GuiButton, GuiCheckBox,...) are all derived from GuiElement (so are "sister" of class Mother):

class Child : public Mother
{
virtual void Drag(int x, int y)
{
/* <- Drag me! -> */
}
}




You have so a tree: GuiButton, GuiCheckBox are all leaf, GuiElement is the root of the tree.

You'll do somewhere:
GuiButton * pButton=new GuiButton();
Mother * pWindow=new Mother();
pWindow->Add(pButton); // <- maybe adding some information about position
pWindow->Drag(5,10);

pWindow will call method Drag(5,10) for all the children... that is the result you want.

I hope you see what I mean. :)

P.S.- If you don't use "virtual" in your example, if you use a pointer to Mother you'll call Mother::foo(), with a pointer to a Child you'll call Child::foo():

Mother * pMother=new Child();
ASSERT(pMother->foo()==1337); // counter-intuitive
Child * pChild=new Child();
ASSERT(pChild->foo()==42);

Share this post


Link to post
Share on other sites

This topic is 4728 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this