Newbie question about virtual functions

J

Jim

Hi, can someone explain to me if virtual functions need to be invoked
by a pointer to the object (not just a normal object) in order to
facilitate polymorphism?

So, in this example:

class BaseClass
{
...
virtual vfunc();
...
}

class DervicedClass
{
...
vfunc();
...
}

BaseClass b;
DerviedClass d;
b = d;
b.vfunc();

Will DerivedClass's vfunc be called?

Thanks in advance.
 
V

Victor Bazarov

Jim said:
Hi, can someone explain to me if virtual functions need to be invoked
by a pointer to the object (not just a normal object) in order to
facilitate polymorphism?

It doesn't.
So, in this example:

class BaseClass
{
...
virtual vfunc();

virtual void vfunc();
;
class DervicedClass

class DerivedClass : public BaseClass
{
...
vfunc();

void vfunc();
;
BaseClass b;
DerviedClass d;
b = d;
b.vfunc();

Will DerivedClass's vfunc be called?

Of course not. 'b' has the type 'BaseClass'.

V
 
G

Gavin Deane

Jim said:
Hi, can someone explain to me if virtual functions need to be invoked
by a pointer to the object (not just a normal object) in order to
facilitate polymorphism?

A pointer or reference, yes.
So, in this example:

class BaseClass
{
...
virtual vfunc();
...
}

class DervicedClass
{
...
vfunc();
...
}

BaseClass b;
DerviedClass d;
b = d;
b.vfunc();

Will DerivedClass's vfunc be called?

No. b is of type BaseClass. When you did

b = d;

you did what is called slicing. You sliced off the BaseClass part of
the DerivedClass object d and assigned it to the BaseClass object b.
This is unlikely to be what you want and should usually be avoided.

A BaseClass object can only ever be a BaseClass object. It can not be a
DerivedClass object "under the hood".

An object of type pointer-to-BaseClass
BaseClass* pb
can actually point to a on object of type DerivedClass, and similarly
for a reference
BaseClass& rb

Gavin Deane
 
M

Marcus Kwok

Gavin Deane said:
No. b is of type BaseClass. When you did

b = d;

you did what is called slicing. You sliced off the BaseClass part of
the DerivedClass object d and assigned it to the BaseClass object b.

I always interpreted it as the other way around: The DerivedClass part
of d gets sliced off, leaving only the BaseClass part, which gets
assigned to b.
 
T

Tom Widmer

Jim said:
Hi, can someone explain to me if virtual functions need to be invoked
by a pointer to the object (not just a normal object) in order to
facilitate polymorphism?

So, in this example:

class BaseClass
{
...
virtual vfunc();
...
}

class DervicedClass
{
...
vfunc();
...
}

BaseClass b;
DerviedClass d;
b = d;
b.vfunc();

Will DerivedClass's vfunc be called?

No. b above is-a BaseClass (you can't change the type of an object once
it has been created). When you assign the d object to it, BaseClass&
operator=(BaseClass const&) is called - e.g. only the BaseClass part of
d is assigned. This is called 'slicing'.

For polymorphism, you need pointers or references.

Tom
 
V

Victor Bazarov

Marcus said:
I always interpreted it as the other way around: The DerivedClass
part of d gets sliced off, leaving only the BaseClass part, which gets
assigned to b.

There is no "DerivedClass part". DerivedClass instance includes BaseClass
as its part. IOW, "DerivedClass" is _larger_ than "BaseClass", so one
can't use the expression "DerivedClass part" when talking of the instance.
I know, I know, you probably want it to mean "The part that DerivedClass
adds to BaseClass", but it just comes out wrong.

V
 
M

Marcus Kwok

Victor Bazarov said:
There is no "DerivedClass part". DerivedClass instance includes BaseClass
as its part. IOW, "DerivedClass" is _larger_ than "BaseClass", so one
can't use the expression "DerivedClass part" when talking of the instance.
I know, I know, you probably want it to mean "The part that DerivedClass
adds to BaseClass", but it just comes out wrong.

Yes. How about, "The DerivedClass-only part"?
 
J

Jim

Okay, this make more sense now.

This may be a stupid question, but why doesn't slicing occur with
pointers or references? Is it because the pointer just contains a
memory address and not the actual object?

Thanks for the responses.
 
T

Tom Widmer

Jim said:
Okay, this make more sense now.

This may be a stupid question, but why doesn't slicing occur with
pointers or references? Is it because the pointer just contains a
memory address and not the actual object?

Slicing only occurs when you make a copy of an object. If you are just
assigning its address to a pointer, or binding it to a reference, no
copying of the object occurs.

e.g.

B b;
A a(b); //b is "sliced" and it's A part is copied into a.


A* ap = &b; //no copying of b
A& ar = b; //no copying of b

Tom
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

Jim said:
Okay, this make more sense now.

This may be a stupid question, but why doesn't slicing occur with
pointers or references? Is it because the pointer just contains a
memory address and not the actual object?

That's how I think of it...


Best regards / Med venlig hilsen
Martin Jørgensen
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top