Virtual functions

R

Richard Hayden

Hi,

I have a question regarding the type of the 'this' reference for virtual
functions.

Say for example, you have:

class C1 {
....
public:
virtual void func() { ... }
....
};

class C2 : public C1 {
....
// Assume C2 does not override func
....
};

Say you then create a C2 and call the 'func()' function on it via a
reference to C1, does the 'this' reference in func() have type
'reference to C1' or 'reference to C2'? Clearly, if I had overridden
func in C2, it would obviously be 'reference to C2', but does this also
hold even if func is not overridden in C2? This is probably pretty much
a non-question since it doesn't at first appear as if there would
actually be a situation where it would matter, however, I was thinking
from the POV of overload resolution, when the implied object parameter
is considered.

Thanks,

Richard Hayden.
 
R

Rob Williscroft

Richard Hayden wrote in in
comp.lang.c++:
Hi,

I have a question regarding the type of the 'this' reference for
virtual functions.

Say for example, you have:

class C1 {
...
public:
virtual void func() { ... }
...
};

class C2 : public C1 {
...
// Assume C2 does not override func
...
};

Say you then create a C2 and call the 'func()' function on it via a
reference to C1, does the 'this' reference in func() have type
'reference to C1' or 'reference to C2'? Clearly,

'this' is a pointer not a reference.

However in C1::func() the this pointer points to a C1 or to
a C1 sub-object of a derived class.
if I had overridden
func in C2, it would obviously be 'reference to C2', but does this
also hold even if func is not overridden in C2?

Well 'it' in this case would be a different function C2::func().
This is probably pretty much
a non-question since it doesn't at first appear as if there would
actually be a situation where it would matter, however, I was thinking
from the POV of overload resolution, when the implied object parameter
is considered.

Don't see what that has to do with it. Overload resolution is done
with the *static* type of the object, once overload resolution is
complete (i.e. a member has been selected) then if its a virtual
member the the member is called based on the *dynamic* type of
the object.

Rob.
 
J

John Harrison

Hi,

I have a question regarding the type of the 'this' reference for virtual
functions.

Say for example, you have:

class C1 {
...
public:
virtual void func() { ... }
...
};

class C2 : public C1 {
...
// Assume C2 does not override func
...
};

Say you then create a C2 and call the 'func()' function on it via a
reference to C1, does the 'this' reference in func() have type
'reference to C1' or 'reference to C2'? Clearly, if I had overridden
func in C2, it would obviously be 'reference to C2', but does this also
hold even if func is not overridden in C2? This is probably pretty much
a non-question since it doesn't at first appear as if there would
actually be a situation where it would matter, however, I was thinking
from the POV of overload resolution, when the implied object parameter
is considered.

Thanks,

Richard Hayden.

The this pointer would point to C2 in that case. It certainly would matter
if you called another virtual function, or if you used dynamic_cast.

To be precise, one would say that the static type of this is C1* but its
dynamic or run time type is C2*.

It's really no different from this situation

C1* ptr = new C2;

ptr has a static type of C1* and a dynamic type of C2*.

john
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top