Dereferencing this in constructors initialiser list

Discussion in 'C++' started by Martin Zimmermann, Apr 14, 2004.

  1. Hi.

    Is it allowed to dereference 'this' in a constructors initialiser list
    to initialize a reference? (for later use)

    Is this code legal or illegal?

    class Foo;

    class Bar
    {
    public:
    Bar(Foo& foo)
    : foo_(foo)
    {
    }
    private:
    Foo& foo_;
    };

    class Foo
    {
    public:
    Foo()
    : bar_(*this)
    {
    }
    private:
    Bar bar_;
    };

    int main()
    {
    }

    Bye.
     
    Martin Zimmermann, Apr 14, 2004
    #1
    1. Advertising

  2. Martin Zimmermann

    tom_usenet Guest

    On Wed, 14 Apr 2004 12:02:13 +0200, Martin Zimmermann
    <> wrote:

    >Hi.
    >
    >Is it allowed to dereference 'this' in a constructors initialiser list
    >to initialize a reference? (for later use)


    Yes. You can even do that before the constructor has even started. You
    can only use that lvalue to access things that have already been
    initialized (such as earlier base classes, earlier member variables
    (in class declaration order)).

    This is perfectly valid:

    Foo* mem = static_cast<Foo*>(malloc(sizeof *mem));
    assert(mem);
    Foo& f = *mem;
    //don't use f until it has been constructed!
    new(mem) Foo();
    //now you can use f safely.

    Tom
    --
    C++ FAQ: http://www.parashift.com/c -faq-lite/
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
     
    tom_usenet, Apr 14, 2004
    #2
    1. Advertising

  3. Martin Zimmermann wrote in news:c5j26s$369$02$-online.com in
    comp.lang.c++:

    > Hi.
    >
    > Is it allowed to dereference 'this' in a constructors initialiser list
    > to initialize a reference? (for later use)
    >
    > Is this code legal or illegal?
    >
    > class Foo;
    >
    > class Bar
    > {
    > public:
    > Bar(Foo& foo)
    > : foo_(foo)
    > {
    > }
    > private:
    > Foo& foo_;
    > };
    >
    > class Foo
    > {
    > public:
    > Foo()
    > : bar_(*this)
    > {
    > }
    > private:
    > Bar bar_;
    > };
    >
    > int main()
    > {
    > }
    >
    > Bye.


    Simple logic should suffice. In order to initialize the bar_
    subobject 'this' must be dereferenced (this->bar_) anyway,
    so yes you can.

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, Apr 14, 2004
    #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. Dave Rudolf
    Replies:
    12
    Views:
    8,304
    Martijn Lievaart
    Feb 6, 2004
  2. Jeremy Smith
    Replies:
    2
    Views:
    590
    Jeremy Smith
    Aug 3, 2006
  3. Jess
    Replies:
    5
    Views:
    609
    Ron Natalie
    Jun 7, 2007
  4. Peng Yu
    Replies:
    5
    Views:
    399
    Juha Nieminen
    Sep 19, 2008
  5. srp113
    Replies:
    3
    Views:
    470
Loading...

Share This Page