H
Harvey J Cohen, Ph. D.
I've been using STL for some time, but I have discovered an issue that
I'm sure has been resolved, but I'm not sure on the proper way to use STL
to solve this issue.
The issue is inheritance and the use of STL containers.
Consider a base class Fruit, with derived classes Orange, Banana and
Apple.
I want to create an STL container of "Fruits", which may be Orange,
Banana or Apple. I don't have any generic Fruit.
I create the container (lets say a deque) as follows:
deque<Fruit> theCrate;
In my application, I create anOrange, aBanana and anApple as follows:
Banana aBanana;
Orange anOrange;
Apple anApple;
Lets say that class Fruit has a virtual method "Eat". I declare a
virtual Eat for each derived class of Fruit (that is, for Banana, Apple
and Orange).
I add instances of Orange, Apple and Banana to my deque as follows:
theCrate.push_back (anOrange);
theCrate.push_back (aBanana);
theCrate.push_back (anApple);
if I perform the following code
for (deque<Fruit>::iterator pF = theCrate.begin ()
pF != theCrate.end ();
pF++)
pF->Eat ();
What I get is three executions of the method Fruit::Eat.
The fact that anOrange, aBanana and anApple are derived from Fruit, but
not a fruit, is lost on STL.
What is happening is that STL is enforcing using the Fruit contructor,
not the Orange, Banana or Apple constructors, and the nature of each
fruit is lost.
If I use the following container
deque<Fruit*> theCrate,
and store points to aBanana, anOrange and anApple, when I execute
(*pF)->Eat ();
I get executions of Banana::Eat, Orange::Eat and Apple::Eat.
Is there any way around this problem? I don't want to store pointers.
Regards,
Harvey
I'm sure has been resolved, but I'm not sure on the proper way to use STL
to solve this issue.
The issue is inheritance and the use of STL containers.
Consider a base class Fruit, with derived classes Orange, Banana and
Apple.
I want to create an STL container of "Fruits", which may be Orange,
Banana or Apple. I don't have any generic Fruit.
I create the container (lets say a deque) as follows:
deque<Fruit> theCrate;
In my application, I create anOrange, aBanana and anApple as follows:
Banana aBanana;
Orange anOrange;
Apple anApple;
Lets say that class Fruit has a virtual method "Eat". I declare a
virtual Eat for each derived class of Fruit (that is, for Banana, Apple
and Orange).
I add instances of Orange, Apple and Banana to my deque as follows:
theCrate.push_back (anOrange);
theCrate.push_back (aBanana);
theCrate.push_back (anApple);
if I perform the following code
for (deque<Fruit>::iterator pF = theCrate.begin ()
pF != theCrate.end ();
pF++)
pF->Eat ();
What I get is three executions of the method Fruit::Eat.
The fact that anOrange, aBanana and anApple are derived from Fruit, but
not a fruit, is lost on STL.
What is happening is that STL is enforcing using the Fruit contructor,
not the Orange, Banana or Apple constructors, and the nature of each
fruit is lost.
If I use the following container
deque<Fruit*> theCrate,
and store points to aBanana, anOrange and anApple, when I execute
(*pF)->Eat ();
I get executions of Banana::Eat, Orange::Eat and Apple::Eat.
Is there any way around this problem? I don't want to store pointers.
Regards,
Harvey