polymorphic call via this pointer stored in base class constructor

A

andy_P

Hi,

Is it OK to use "this" pointer stored in base class constructor to
make a polymorphic function calls?
I am doing something like this and expect that derived m() is called
in test().

class T
{
virtual void exec(int a)=0;
};

class Base
{
protected:
static const int Base_a = 10;
class TT : public T
{
Base* owner;
public:
void set_owner(Base* o){owner = o;}
virtual void exec(int a)
{owner->m(a);}
};
TT t;
int _a;

virtual void m(int a)
{_a = a;}

public:
Base(){t.set_owner(this);}
void test()
{
t.exec(Base_a);
}
};

class D : public Base
{

protected:
int _b;
virtual void m(int x)
{_b = x; }
public:

};

int main()
{
D dd;
dd.test();
return 0;
}

Thank you.
Andy.
 
J

Juha Nieminen

andy_P said:
Is it OK to use "this" pointer stored in base class constructor to
make a polymorphic function calls?

Why would there be a difference between the 'this' pointer and any
other pointer pointing to the same object? It's not like the language
acts differently if the pointer happens to be the compiler-generated
'this' pointer.
 
R

Robert Hairgrove

Juha said:
Why would there be a difference between the 'this' pointer and any
other pointer pointing to the same object? It's not like the language
acts differently if the pointer happens to be the compiler-generated
'this' pointer.

If you call the function in the body of the base class constructor (or
initialisation list) through the "this" pointer, it probably won't do
what you want it to do ... if it is a pure virtual function, you will
get an error, and if it has an implementation in the base class, that is
what will be called because the base class part of the object is always
instantiated first before any other members in derived classes have been
constructed.

However, it is perfectly all right to store the "this" pointer in a
class member and call it later. Some compilers issue a warning about
this which you can safely ignore. It should work OK -- unless you are
calling it from the constructor of the derived class, in which case it
probably won't do what you expect.
 
J

Juha Nieminen

Robert Hairgrove said:
If you call the function in the body of the base class constructor (or
initialisation list) through the "this" pointer, it probably won't do
what you want it to do ... if it is a pure virtual function, you will
get an error, and if it has an implementation in the base class, that is
what will be called because the base class part of the object is always
instantiated first before any other members in derived classes have been
constructed.

I really don't understand what the 'this' pointer has to do with what
you wrote above. What you are writing is a feature related to base class
constructors calling virtual functions (regardless of how they are
called). It has nothing to do with whether you use the 'this' pointer or
not for that.

Maybe what you mean is that if you make a base class constructor which
takes a pointer of the base class type as parameter and uses that pointer
to call a virtual function assuming that the pointer is pointing to a
*different* object (which has already been constructed), then it will
cause problems if you give it a pointer to itself, because the object has
not been fully constructed yet. In that case, yes, the virtual function
call won't work properly because the derived part is still unconstructed.
It just confusing to say that the "base class constructor calls the
virtual function through the 'this' pointer", because it sounds like it's
explicitly saying "this->foo();" (which is no different from a simple
"foo();", the 'this' pointer in itself is not the relevant issue here).
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top