what happends when... (polymorphism question)

Started by
12 comments, last by graveyard filla 19 years, 5 months ago
hi, lets say we had these 3 classes:

class Parent
{
  public:
  
  ...
   void Hello(Son *p);
   void Hello(Daughter *p);
   void Hello(Parent   *p);
};

class Son : public Parent
{
  ...
};

class Daughter : public Parent
{
  ..
};


now, what happends if we do this:

Parent Dad,Mom;
dad.Hello(&Mom);
what happends here? which function is called?
FTA, my 2D futuristic action MMORPG
Advertisement
Did I miss something? Why wouldn't Parent::Hello(Parent*) be called?

Also you would have to declare Son and Daughter before referencing them in Parent, like so:
class Son;class Daughter;class Parent{//...};class Son : public Parent{//...};class Daughter : public Parent{//...};
hi Rayno,

hmmm, maybe i gave a bad example. what would happen if i did this?

Son son;Parent dad;dad.Hello(&son);


which function would it call then?

thanks again.
FTA, my 2D futuristic action MMORPG
Hello(Son *s). you're simply overloading the function Hello with different class types as arguments. you could even do :
Parent* a = new Parent();Parent* b = new Parent();a->Hello((Son*)b); // calls Hello(Son* p);// orParent* a = new Parent();Son*    b = new Son();b->Hello((Daughter*)a); // probably should use static_cast
- stormrunner
hi stormrunner,

heh, its been awhile since ive thought about this stuff... but i don't understand your first example. how is it possible for a parent to turn into a child pointer? i thought only a child could be used as a parent, and not vice-versa. i mean, what if Son has members which Parent doesn't? if you cast the parent to a son, i just dont see how this is possible because then the function could mess with Son:: members when the pointer really belongs to a parent, who doesnt have those members. i just dont see how that works out... i mean, my only guess is that those members are only temporary and when the function returns it loses any child members. heh, didnt even know about that.. i guess its about time i learned about the casting of pointers in situations like this, i've been putting it off for awhile because i haven't needed it [smile]

thanks for any help.



[Edited by - graveyard filla on October 25, 2004 12:23:07 AM]
FTA, my 2D futuristic action MMORPG
i gotta run, but heres my hacked demo. i tested it with dev just to be sure.
#include <iostream>class b;class c;class a{    public:        void A(a* _a)        {            std::cout << "a* called" << std::endl;        }        void A(b* _a)        {            std::cout << "b* called" << std::endl;        }        void A(c* _a)        {            std::cout << "c* called" << std::endl;        }        void printA(void)        {            std::cout << "print from a" << std::endl;        }};class b : public a{/* just inherits everything from a */};class c : public a{    public:        void access(c* _a)        {                _a->print();        }        void print(void)        {            std::cout << "i can print" << std::endl;        }};int main(){    a* _A = new a();    a* _B = new a();    _A->A(_B); // calls (b*)    _A->A((c*)_B); // cast to a child (c*)    c* _C = new c(); // a new child : a    _C->A((b*)_A); // cast to a child - calls (b*)    _C->access((c*)_A); // cast to a child - calls (c*)    // calls class [c] specific function    _A->A((a*)_C); // cast to a parent - calls (a*)    // uses class [a] specific function    int a;    std::cin >> a;	return(0);}
- stormrunner
Graveyard,

I fail to see where your confusion lies. You're deriving a child class from a parent class, then calling a function on the child class. That's pretty basic. There's no polymorphism here.

The hello function will always resolve to the highest level of the parameters.

so...

Son son;
Parent dad;
dad.Hello(&son);

Will just call the hello function on the Parent object which took Son* as a parameter.

ie.
Parent::Hello( Son* p );

Nothing you have here is declared virtual, so it is impossible for you to get any polymorphism out of this.

Polymorphism is used to call the function of a derived class when you have a pointer to the base class...

Example:

class Parent
{
public:
virtual void HellO();
};

class Child : public Parent
{
public:
virtual void Hello();
}
-----
Parent* p = new Child;

In this case you've got what's called "A base pointer to a derived class". Since Hello is virtual, calling

p->Hello();

Will actually call Child::Hello(). That's polymorphism.

Make any sense?
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
i guess my main confusion was i didn't understand which function would be called when the parameters were ambiguous. for example, lets say that the Hello(Son *p); function wasn't a member of the Parent class. if i did:

Parent Dad;
Son son;

Dad.Hello(&son);

this would call the Hello(Parent*) version of the function, no? i guess i just didn't understand which function it would call when both functions where defined. isnt this considered polymorphism when the son pointer is treated as a Parent pointer? i guess thats what i thought, anyway [smile]. i already understand the purpose of and how to use virtual functions though. thanks again.

@stormrunner

thanks, ill check that out.
FTA, my 2D futuristic action MMORPG
graveyard,

Now I understand your question. When a class defines multiple functions, each with the same name, but taking different parameters, its called function overloading. This you probably already knew.

If the parameters happen to be derived from each other, the compiler doesnt care. It will first try and resolve it against the class which is being passed in. If it cant, it'll try and find a compatible type, such as a parent.

So if Child derives from Parent as in our previous examples..and a function is defined which takes both a Parent and a Child, then passing in a Child will call the one which expects a child. However, if there is no function which takes a child, it will call the one that expects a parent.

And no, treating a child pointer as a base pointer isn't polymorphism. Its just a benefit of Inheritance. Polymorphism as I said before is when you have a base pointer to a derived class, and through the use of virtual functions, are still able to call child functions. Cheers and Good Luck!
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
thanks J, that was the answer i was looking for.

what about in this situation?

Parent mom,*dad;

dad = new Son();

mom.Hello(dad);

will it call the *Son version here, or the Parent version? i totally forgot to ask this and this was my main question =).
FTA, my 2D futuristic action MMORPG

This topic is closed to new replies.

Advertisement