polymorphic call via this pointer stored in base class constructor

Discussion in 'C++' started by andy_P, Nov 14, 2010.

  1. andy_P

    andy_P Guest

    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.
    andy_P, Nov 14, 2010
    #1
    1. Advertising

  2. andy_P <> 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.
    Juha Nieminen, Nov 14, 2010
    #2
    1. Advertising

  3. Juha Nieminen wrote:
    > andy_P <> 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.
    Robert Hairgrove, Nov 14, 2010
    #3
  4. andy_P

    andy_P Guest

    Thank you, Juha, Robert

    I guess I may have overthought it a bit.
    andy_P, Nov 14, 2010
    #4
  5. Robert Hairgrove <> 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).
    Juha Nieminen, Nov 15, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. S. Levent Yilmaz
    Replies:
    5
    Views:
    3,731
    Peter Koch Larsen
    Nov 26, 2003
  2. mrstephengross
    Replies:
    5
    Views:
    593
    Larry I Smith
    May 18, 2005
  3. ali
    Replies:
    4
    Views:
    571
    David Harmon
    Mar 5, 2007
  4. Rahul
    Replies:
    16
    Views:
    1,196
    terminator
    Nov 7, 2007
  5. awm129
    Replies:
    6
    Views:
    1,128
Loading...

Share This Page