Polymorphic types and a lot of confusion....

P

pmatos

Hi all,

I think I'm suffering from some confusion with polymorphic types by
C++.
Imagine the following example:
I have the ABC fruit and then the classes Pear, Apple, Banana that
inherit from fruit. The I create a list of fruits. Now, I wish to
tranverse the list of fruits and now I have op1(banana * b), op2(apple
* a) and op3(pear * p) which should be called for type of fruit. The
problem is that I get an error during compile time since it tells me
op1, op2, and neither op3 receive a fruit*, well, that I understand. I
do not understand is how to solve this issue. What's the best solution?
A cast or something like that?

Cheers,

Paulo Matos
 
M

Micha

pmatos said:
do not understand is how to solve this issue. What's the best solution?
A cast or something like that?

in your base class you define a virtual method op(fruit*) = 0

Each of your derived fruit-classes gets its own virtual op(apples*) method
(or banana* ... ).

Then you can walk through the list and call the op - method on each member.

Michael
 
P

Peter Koch Larsen

pmatos said:
Hi all,

I think I'm suffering from some confusion with polymorphic types by
C++.
Imagine the following example:
I have the ABC fruit and then the classes Pear, Apple, Banana that
inherit from fruit. The I create a list of fruits. Now, I wish to
tranverse the list of fruits and now I have op1(banana * b), op2(apple
* a) and op3(pear * p) which should be called for type of fruit. The
problem is that I get an error during compile time since it tells me
op1, op2, and neither op3 receive a fruit*, well, that I understand. I
do not understand is how to solve this issue. What's the best solution?
A cast or something like that?

Cheers,

Paulo Matos
Apart from what Micha wrote, remember that a std::list (as all containers in
the standard library) is valuebased. Thus, if you store a pear in a
std::list<fruit>, the object will get sliced. Only the fruit-part will be
stored. You will thus need to have a list of fruit* or - better - a list of
e.g. boost::shared_ptr<fruit>.

/Peter
 
D

Daniel T.

pmatos said:
Hi all,

I think I'm suffering from some confusion with polymorphic types by
C++.
Imagine the following example:
I have the ABC fruit and then the classes Pear, Apple, Banana that
inherit from fruit. The I create a list of fruits. Now, I wish to
tranverse the list of fruits and now I have op1(banana * b), op2(apple
* a) and op3(pear * p) which should be called for type of fruit. The
problem is that I get an error during compile time since it tells me
op1, op2, and neither op3 receive a fruit*, well, that I understand. I
do not understand is how to solve this issue. What's the best solution?
A cast or something like that?

class fruit {
public:
virtual void op() = 0;
};

class apple : public fruit {
public:
void op() { op2( this ); }
};

// &c.

for_each( myFruits.begin(), myFruits.end(), mem_fun( &fruit::eek:p ) );
 
H

Howard

Peter Koch Larsen said:
Apart from what Micha wrote, remember that a std::list (as all containers
in the standard library) is valuebased. Thus, if you store a pear in a
std::list<fruit>, the object will get sliced. Only the fruit-part will be
stored. You will thus need to have a list of fruit* or - better - a list
of e.g. boost::shared_ptr<fruit>.

/Peter

Yum! Sliced fruit...my favorite! :)

-H
 
M

Marcin Kalicinski

Hi,

This is a well-known object oriented programming problem. There are 2 common
ways to cope with it: one is by use of virtual function in base class, which
is basically what everybody already described to you in other replies.

The other way is the visitor pattern. If you're interested I'm pretty sure
you'll find tons of information on that just by asking google about "visitor
pattern". You might also lay your hands on Gamma et al book on patterns,
which contains even more goodies like that.

cheers,
Marcin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,831
Latest member
HealthSmartketoReviews

Latest Threads

Top