Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > polymorphic call via this pointer stored in base class constructor

Reply
Thread Tools

polymorphic call via this pointer stored in base class constructor

 
 
andy_P
Guest
Posts: n/a
 
      11-14-2010
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.
 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      11-14-2010
andy_P <(E-Mail Removed)> wrote:
> 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.
 
Reply With Quote
 
 
 
 
Robert Hairgrove
Guest
Posts: n/a
 
      11-14-2010
Juha Nieminen wrote:
> andy_P <(E-Mail Removed)> wrote:
>> 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.


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.
 
Reply With Quote
 
andy_P
Guest
Posts: n/a
 
      11-14-2010
Thank you, Juha, Robert

I guess I may have overthought it a bit.

 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      11-15-2010
Robert Hairgrove <(E-Mail Removed)> wrote:
> 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).
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
call base class constructor from derived class constructor Rahul C++ 16 11-07-2007 03:40 PM
Calling base class constructor from derived class Copy constructor ali C++ 4 03-05-2007 09:15 AM
Copy constructor for a class that contains a pointer to a base class type (newbie) SzH C++ 1 10-27-2006 09:21 PM
Invoking templatized base class constructor from templatized derived class constructor mrstephengross C++ 5 05-18-2005 07:12 PM
polymorphic call in abstract class's constructor S. Levent Yilmaz C++ 5 11-26-2003 10:32 AM



Advertisments