[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Understanding virtual functions

Barry

10/9/2008 7:27:00 AM

Lets say I have the following -

class Shape
{
...
virtual void Plot();
PrintInfo();
}

class Circle : public Shape
{
...
void Plot();
PrintInfo();
}

....

Are the following correct? -

Circle circle = new Circle();
Shape* pShape = circle;
Circle* pCircle = circle;

circle.Plot(); // Circle's Plot
circle.PrintInfo(); // Circle's PrintInfo

pCircle->Plot(); // Circle's Plot
pCircle->PrintInfo(); // Circle's PrintInfo

pShape->Plot(); // Circle's Plot
pShape->PrintInfo(); // Shape's PrintInfo

((Shape)circle).Plot(); // Shape's Plot
((Shape)circle).PrintInfo(); // Shape's PrintInfo

((Circle*)pShape)->Plot(); // Circle's PrintInfo
((Circle*)pShape)->PrintInfo(); // Circle's PrintInfo

((Shape*)pCircle)->Plot(); // Circle's PrintInfo
((Shape*)pCircle)->PrintInfo(); // Shape's PrintInfo

Thanks,

Barry.
3 Answers

Matthias Berndt

10/9/2008 9:55:00 AM

0

Are you too dumb or too lazy to just try?

gw7rib

10/14/2008 7:29:00 PM

0

On 9 Oct, 10:54, Matthias Berndt <matthias_ber...@gmx.de> wrote:
> Are you too dumb or too lazy to just try?

To be fair, "just trying" is not a very good way of finding out how C+
+ works. Things may appear to work misleadingly. Before you know it,
people start saying things like "a = a++; will increment a" or "void
main is fine", becasue they've tried it and found that it works. They
think.

gw7rib

10/14/2008 7:55:00 PM

0

On 9 Oct, 08:27, Magnus.Morab...@gmail.com wrote:

Well, if no-one else will reply to this...

> Lets say I have the following -
>
> class Shape
> {
>   ...
>   virtual void Plot();
>   PrintInfo();
>
> }

Presumably the "..." includes a "public:". And you need a type for the
return value of PrintInfo. And a semicolon after the "{".

> class Circle : public Shape
> {
>   ...
>   void Plot();
>   PrintInfo();
>
> }
>
> ...
>
> Are the following correct? -
>
> Circle circle = new Circle();

This won't work. "new" gives you a pointer to an object. So you would
need something like either:

Circle* circle = new Circle();

or

Circle circle;

> Shape* pShape = circle;
> Circle* pCircle = circle;

If you've gone for the second approach you need &circle, not circle.

> circle.Plot();        // Circle's Plot
> circle.PrintInfo();  // Circle's PrintInfo

If you've gone for the second approach then yes, this looks right.

> pCircle->Plot();        // Circle's Plot
> pCircle->PrintInfo();  // Circle's PrintInfo

Yes.

> pShape->Plot();       // Circle's Plot

Yes. This is what virtual functions do.

> pShape->PrintInfo(); // Shape's PrintInfo

Yes. However, if you find yourself doing this sort of thing in real
code then there is probably something badly wrong with your design.

> ((Shape)circle).Plot();        // Shape's Plot
> ((Shape)circle).PrintInfo();  // Shape's PrintInfo

Before I tried it, I wasn't at all convinced these casts would even
work. But they seem to.

> ((Circle*)pShape)->Plot();      // Circle's PrintInfo
> ((Circle*)pShape)->PrintInfo(); // Circle's PrintInfo
>
> ((Shape*)pCircle)->Plot();      // Circle's PrintInfo
> ((Shape*)pCircle)->PrintInfo(); // Shape's PrintInfo

Er, yes. But I don't think that messing about with casts like this is
really going to help you understand what is going on.

Hope that is useful.
Paul.