Does the 'this' pointer do the virtual function goodness?

Status
Not open for further replies.

Screndib

Ars Tribunus Militum
2,038
I ask because it doesn't seem to be for me. I've got a number of classes that inherit from a base class with some virtual functions. When I have a base class pointer everything works fine as it should. When I'm using the this pointer in the base class to call a virtual function it basically ignores the virtual part and treats the class as just normal inheiratance.

Is this the normal behavior for the this pointer? Seems kind of lame to me, what's the reasoning behind it (assuming this is the normal behavior)?

edit: I'll give an example for clairity

code:<HR>
Code:
class A
{
public:
    virtual PrintMessage( void ) { cout << "Class A" << endl; }
    virtual DoSomething( void )
    {
        this->PrintMessage();
    }
};

class B : public A
{
public:
    virtual PrintMessage( void ) { cout << "Class B" << endl; }
};



A *pointer = new B;
pointer->PrintMessage();
pointer->DoSomething();

This would print:
code:<HR>
Code:
Class B
Class A

[This message was edited by Screndib on February 24, 2002 at 09:51.]
 

Richard Berg

Ars Legatus Legionis
43,037
Subscriptor
[Obi-Wan]
These aren't the pointers you're looking for.
[/Obi-Wan]

I couldn't find a good FAQ for you; ]this entry is closest. Basically, the way the compiler handles inheritance, creating a Derived object also creates a Base object (that's why you can call the Base constructor in Derived's constructor, for instance). Calls to virtual functions have to be resolved by a jump table as to which (physically distinct) object they refer to; that pointer is then placed in "this".

Edit: apparently it's not only unexpected but nonstandard; hence my looking for a good FAQ like the comp.lang.c one that covers things like why i=i++ is never a good idea.

[This message was edited by Richard Berg on February 24, 2002 at 11:47.]
 
Mac os X
gcc version: Apple Computer, Inc. version gcc-934.3, based on gcc version 2.95.2 19991024 (release)

Code:
code:<HR>
Code:
#include <iostream>

class A
{
public:
virtual void PrintMessage( void ) { cout << "Class A" << endl; }
virtual void DoSomething( void )
{
this->PrintMessage();
}
};

class B : public A
{
public:
virtual void PrintMessage( void ) { cout << "Class B" << endl; }
};

int main()
{
A *pointer = new B;
pointer->PrintMessage();
pointer->DoSomething();
return 0;
}

I get
code:<HR>
Code:
[merle:~/code] dolapo% c++ -o virtual virtual.C
[merle:~/code] dolapo% ./virtual 
Class B
Class B
 
Nevertheless, I'd be surprised if the proper behavior for your example code above were standardized; any C++ gurus know for sure?
this is polymorphic just like any pointer, so what Dr Steelface posted is what any compiler should yield. Also, there is no need to make the call by explicitely using this. Something like:
code:<HR>
Code:
virtual DoSomething( void )
{
    PrintMessage();
}
Works the same. DoSomething() doesn't need to be virtual either.
 
Status
Not open for further replies.