dynamic binding and this pointer

Discussion in 'C++' started by rong, May 9, 2009.

  1. rong

    rong Guest

    Hi,

    I have some puzzle on what "this" pointer really points in
    constructor, in particular in the case of inheritance - explained in
    the following sample


    #include <iostream>
    #include <string>

    using namespace std;

    class Base;
    static string getType(Base *p);

    class Base {
    public:
    Base() { cout << "Type: " << getType(this) << endl; }
    void showType() { cout << "Type: " << getType(this) << endl; }

    virtual string type() { return string("Base"); }
    };

    class Derived : public Base {
    public:
    string type() { return string("Derived"); }
    };

    static string getType(Base *p)
    {
    return p->type();
    }

    int main(int argc, char **argv)
    {
    Derived foo;

    foo.showType();
    return 0;
    }


    I was expecting both lines printed should be "Type: Derived", but it
    actually prints out

    Type: Base
    Type: Derived

    Any idea? Thanks.


    Regards,

    Rong
    rong, May 9, 2009
    #1
    1. Advertising

  2. rong

    Guest

    On May 8, 8:15 pm, rong <> wrote:
    > Hi,
    >
    > I have some puzzle on what "this" pointer really points in
    > constructor, in particular in the case of inheritance - explained in
    > the following sample
    >
    > #include <iostream>
    > #include <string>
    >
    > using namespace std;
    >
    > class Base;
    > static string getType(Base *p);
    >
    > class Base {
    > public:
    >         Base() { cout << "Type: " << getType(this) << endl; }
    >         void showType() { cout << "Type: " << getType(this) << endl; }
    >
    >         virtual string type() { return string("Base"); }
    >
    > };
    >
    > class Derived : public Base {
    > public:
    >          string type() { return string("Derived"); }
    >
    > };
    >
    > static string getType(Base *p)
    > {
    >         return p->type();
    >
    > }
    >
    > int main(int argc, char **argv)
    > {
    >         Derived foo;
    >
    >         foo.showType();
    >         return 0;
    >
    > }
    >
    > I was expecting both lines printed should be "Type: Derived", but it
    > actually prints out
    >
    > Type: Base
    > Type: Derived
    >
    > Any idea? Thanks.


    Short answer: because the standard says so. The this object does not
    point to a derived object until it enters the derived constructor.

    The rationale is that if a base constructor called a virtual function,
    and this resolved to the implementation in the derived class, then
    this will access an object which has not been constructed. It will not
    be set up, constructors for member sub objects of the derived object
    will not be called. The derived object is in a garbage state until the
    derived constructor is called, so that's why it's done this way.

    I reasonably sure that the same thing happens for destructors too.
    , May 9, 2009
    #2
    1. Advertising

  3. rong

    Guest

    On 9 May, 04:15, rong <> wrote:
    > I have some puzzle on what "this" pointer really points in
    > constructor, in particular in the case of inheritance


    There's more on this in the FAQ - see http://www.parashift.com/c -faq-lite/
    .. Question 23.5 is specifically about this question, but the rest of
    the FAQ is worth reading too.
    , May 9, 2009
    #3
    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. Replies:
    10
    Views:
    676
    Chris Torek
    Feb 4, 2005
  2. jimjim
    Replies:
    16
    Views:
    827
    Jordan Abel
    Mar 28, 2006
  3. Replies:
    4
    Views:
    1,234
    Fred Zwarts
    Jul 2, 2009
  4. A
    Replies:
    7
    Views:
    626
  5. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    445
    James Kuyper
    Sep 23, 2011
Loading...

Share This Page