Jump to content
  • Advertisement
Sign in to follow this  
ryt

When to use polymorphism

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

In which cases I should use polymorphism ?

I know that if we create classes mammal, cat and a dog like:

class mammal
{
	...
	virtual void Run();
};


class cat : public mammal
{ 
	...
	Run();
};


class dog : public mammal	
{
	...
	Run();
};


void main()
{
	mammal *pMammal1;
	mammal *pMammal2;
	cat *pCat = new cat();
	dog *pDog = new dog();

	pMammal1 = pCat;
	pMammal2 = pDog;
}

we can access cat and dog trough mammal. But what is the use of this, when we can implement this ? Why not just put Run() inside cat and dog without having mammal ?

Edited by ryt

Share this post


Link to post
Share on other sites
Advertisement

How can you access this function??? I thought you need inheritance but there is nothing just three seperated classes...

 

you would need something like:

class mammel
{
public:
     virtual void run();
};

class dog : public mammel
{
public:
    virtual void run();
};

class cat : public mammel
{
public:
virtual void run(); 
};

but perhaps i am wrong and just learned it wrong.

 

With polymorphie you recive a behavior "similar" to an array/list of functionpointer which do there job everytime they are called but here instead of an array/list you have 

the class herachie with overrided functions but in case you need an different behavior of an function which was overriden but you need the original with help of polymorphie you could access this function dynamically. // just a very hard simplification

Edited by exOfde

Share this post


Link to post
Share on other sites


Why not just put Run() inside cat and dog without having mammal ?

 

Because then you wouldn't be able to write code that just deals with mammal (i.e. doesn't care if something is a dog or a cat - thus the same code can apply to any Mammal).

Share this post


Link to post
Share on other sites

Lets say you want all your mammals to run, with polymorphism you can put all your mammals in a single mammal array and call their run function. Without polymorphism you'll have to keep a list of dogs, cats etc. and call their run function.

Share this post


Link to post
Share on other sites

How can you access this function??? I thought you need inheritance but there is nothing just three seperated classes...

Yea, I forgot to put public mammal. I fixed it now.

 

So polymorphism is similar to the use of interfaces. If I am not wrong interfaces are classes that contain methods that will be used in multiple other classes, like Run() method here ?

Edited by ryt

Share this post


Link to post
Share on other sites

Lets say you want all your mammals to run, with polymorphism you can put all your mammals in a single mammal array and call their run function. Without polymorphism you'll have to keep a list of dogs, cats etc. and call their run function.

IMHO being able to lazily throw all objects into a huge list to avoid having specialized lists is not sufficient reason for using virtual methods, because the specialized lists could directly store the objects, but the single list would be more complicated in some other way as it needs to store pointers and would necessitate many useless pointer indirections to separately allocated objects and virtual calls.

I think there needs to be some other reason to use virtual methods, for example, being able to hide some low level implementation details or interfacing with library code.

 

I dont really like the example with the mammal, dog and cat, because in a game the code for letting them run would probably be the same for all kinds of living things with legs and a single class could be used for them.

Share this post


Link to post
Share on other sites

 

How can you access this function??? I thought you need inheritance but there is nothing just three seperated classes...

Yea, I forgot to put public mammal. I fixed it now.

 

So polymorphism is similar to the use of interfaces. If I am not wrong interfaces are classes that contain methods that will be used in multiple other classes, like Run() method here ?

 

 

With an interface, every class has to define the method separately even if each has identical code. With a class, you have default behavior (unless you used a pure virtual function) that is only overridden when necessary. So you could have mammal::run() be the same code for every mammal like wintertime mentions, but you could have mammal::makeNoise() result in "meow" for a cat and "woof" for a dog. And if you had a weird mammal that runs differently, you could override run() just for that one mammal without needing to write run() code for every single type of mammal you have defined.

 

I agree with wintertime that you should not use polymorphism just to use it though, and it should be used when it is actually solving a problem that is otherwise difficult to solve.

Share this post


Link to post
Share on other sites


It's especially worth noting that run-time polymorphism is very much possible (and arguably cleaner!) without inheritance -- I strongly recommend watching this:

http://channel9.msdn.com/Events/GoingNative/2013/Inheritance-Is-The-Base-Class-of-Evil

Actually Sean Parent's solution that you mention there does indeed use inheritance - it is just encased into an inner base class that is insulated from the user.  That's strictly a technicality though...

 

I tried this out to implement a form of a mixed object graph, and it works like a charm!  Type erasure for the win!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!