 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Colin Guest
|
Posted: Thu Dec 08, 2005 11:53 am Post subject: Inheritance and virtual functions |
|
|
I'm coding a game that has different types of animations. I want to
have a parent class called Animation, and then inherit classes from it
called things like AnimFoo, AnimBar, etc. that do different types of
animations. I'm thinking the basic skeleton will look like this:
class Animation {
virtual draw();
virtual step();
};
class AnimFoo : public Animation {
draw();
step();
};
etc...
I will have a vector of Animation, and I will put the inherited classes
into it. This way, I can just iterate through one vector calling
draw() and step() to handle all the different types of animations. I
have implemented this:
for (vector< Animation >::iterator i = animations.begin(); i !=
animations.end(); ++i) {
(*i).draw();
(*i).step();
}
But the functions for Animation are being called rather than the
different inherited classes. I tried making the virtual functions pure
virtual, but then it complained about making a vector of abstract
classes. Any ideas?
Thanks,
Colin Kern
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Heinz Ozwirk Guest
|
Posted: Thu Dec 08, 2005 12:57 pm Post subject: Re: Inheritance and virtual functions |
|
|
"Colin" <razael1 (AT) gmail (DOT) com> schrieb im Newsbeitrag
news:1134018950.888486.318290 (AT) f14g2000cwb (DOT) googlegroups.com...
| Quote: | I'm coding a game that has different types of animations. I want to
have a parent class called Animation, and then inherit classes from it
called things like AnimFoo, AnimBar, etc. that do different types of
animations. I'm thinking the basic skeleton will look like this:
class Animation {
virtual draw();
virtual step();
};
class AnimFoo : public Animation {
draw();
step();
};
etc...
I will have a vector of Animation, and I will put the inherited classes
into it. This way, I can just iterate through one vector calling
draw() and step() to handle all the different types of animations. I
have implemented this:
for (vector< Animation >::iterator i = animations.begin(); i !=
animations.end(); ++i) {
(*i).draw();
(*i).step();
}
But the functions for Animation are being called rather than the
different inherited classes. I tried making the virtual functions pure
virtual, but then it complained about making a vector of abstract
classes. Any ideas?
|
When you put instances of derived classes into a vector of base class
objects, only the base class part will be put into your array (slicing).
Polymorphism only works with pointers or references to objects, not with the
objects themselves. What you need is a vector of pointers, like
std::vector<Animation*>
HTH
Heinz
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Hrayr BABAJANYAN Guest
|
Posted: Thu Dec 08, 2005 4:17 pm Post subject: Re: Inheritance and virtual functions |
|
|
Hi!
To use the polymorphic mechanism you should use vector of pointers or
references (e.g. vector<Animation*>).
In your case the instances of derived classes (e.g. AnimFoo) are being
cut during the insertion into the vector<Animation>, so the functions
of the base class Animation are being called.
And that is why if you make Animation abstract, the vector<Animation>
contains only "instances" (cannot instantiate abstract class) of
abstract class, and the compiler complains.
Cheers
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Tutone Guest
|
Posted: Thu Dec 08, 2005 4:18 pm Post subject: Re: Inheritance and virtual functions |
|
|
Heinz Ozwirk wrote:
| Quote: | When you put instances of derived classes into a vector of base class
objects, only the base class part will be put into your array (slicing).
Polymorphism only works with pointers or references to objects, not with the
objects themselves. What you need is a vector of pointers, like
std::vector<Animation*
|
Or better yet, a vector of smart pointers, like
std::vector< std::tr1::shared_ptr
Best regards,
Tom
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Martin Bonner Guest
|
Posted: Thu Dec 08, 2005 4:20 pm Post subject: Re: Inheritance and virtual functions |
|
|
Heinz Ozwirk wrote:
| Quote: | "Colin" <razael1 (AT) gmail (DOT) com> schrieb im Newsbeitrag
news:1134018950.888486.318290 (AT) f14g2000cwb (DOT) googlegroups.com...
I'm coding a game that has different types of animations. I want to
have a parent class called Animation, and then inherit classes from it
called things like AnimFoo, AnimBar, etc. that do different types of
animations.
|
[snip]
| Quote: | When you put instances of derived classes into a vector of base class
objects, only the base class part will be put into your array (slicing).
Polymorphism only works with pointers or references to objects, not with the
objects themselves. What you need is a vector of pointers, like
std::vector<Animation*
|
Or std::vector< boost::shared_ptr if you aren't using
garbage collection (google for boost::shared_ptr)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze Guest
|
Posted: Fri Dec 09, 2005 3:07 pm Post subject: Re: Inheritance and virtual functions |
|
|
Martin Bonner wrote:
| Quote: | Heinz Ozwirk wrote:
"Colin" <razael1 (AT) gmail (DOT) com> schrieb im Newsbeitrag
news:1134018950.888486.318290 (AT) f14g2000cwb (DOT) googlegroups.com...
I'm coding a game that has different types of animations.
I want to have a parent class called Animation, and then
inherit classes from it called things like AnimFoo,
AnimBar, etc. that do different types of animations.
[snip]
When you put instances of derived classes into a vector of
base class objects, only the base class part will be put
into your array (slicing). Polymorphism only works with
pointers or references to objects, not with the objects
themselves. What you need is a vector of pointers, like
std::vector<Animation*
Or std::vector< boost::shared_ptr if you aren't
using garbage collection (google for boost::shared_ptr)
|
You mean there are people who don't use a garbage collector with
C++? :-)
That's http://www.hpl.hp.com/personal/Hans_Boehm/gc/ for the
garbage collector, and http://www.boost.org for Boost (which
you'll need for other things, even if you are using the garbage
collector).
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Colin Guest
|
Posted: Mon Dec 12, 2005 12:08 am Post subject: Re: Inheritance and virtual functions |
|
|
I don't like the extra overhead. It's not that hard to make sure you
destroy anything you've dynamically created.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Joshua Lehrer Guest
|
Posted: Mon Dec 12, 2005 12:07 pm Post subject: Re: Inheritance and virtual functions |
|
|
Heinz Ozwirk wrote:
| Quote: | "Colin" <razael1 (AT) gmail (DOT) com> schrieb im Newsbeitrag
When you put instances of derived classes into a vector of base class
objects, only the base class part will be put into your array (slicing).
Polymorphism only works with pointers or references to objects, not with the
objects themselves. What you need is a vector of pointers, like
std::vector
|
This is a benefit of using type-erasure over inheritence to implement
polymorphism.
Your Animation class could contain a pointer to a function that
implements 'move', 'draw', and any other polymorphic behavior. You can
then store a vector of Animation objects and not have to worry about
slicing.
Google for 'type erasure' for a more detailed explanation.
joshua lehrer
http://www.lehrerfamily.com/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze Guest
|
Posted: Mon Dec 12, 2005 3:12 pm Post subject: Re: Inheritance and virtual functions |
|
|
Colin wrote:
[Talking about garbage collection, I think...]
| Quote: | I don't like the extra overhead.
|
What extra overhead?
| Quote: | It's not that hard to make sure you destroy anything you've
dynamically created.
|
But it's more overhead -- both for me and for the machine, but
especially for me.
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|