*this in initializer list

Discussion in 'C++' started by Alexander Stippler, Mar 18, 2008.

  1. I know using this in a class initializer list has to be dealt with with
    care. I fear for "*this" it is even worse. Is it legal at all - what
    does the standard say?
    My situation is somewhat like:

    struct B;

    struct A
    {
    A(const B &_b) : b(_b) {}

    const B &b;
    };

    struct B
    {
    B() : a(*this) {}
    A a;
    };

    I just want this reference. No member access or similar (in the
    construction process).
    Allowed (and reasonable?) by standard or not?

    best regards,
    Alex
    Alexander Stippler, Mar 18, 2008
    #1
    1. Advertising

  2. Alexander Stippler

    peter koch Guest

    On 18 Mar., 19:03, Alexander Stippler <>
    wrote:
    > I know using this in a class initializer list has to be dealt with with
    > care. I fear for "*this" it is even worse. Is it legal at all - what
    > does the standard say?
    > My situation is somewhat like:
    >
    > struct B;
    >
    > struct A
    > {
    >         A(const B &_b) : b(_b) {}
    >
    >         const B &b;
    >
    > };
    >
    > struct B
    > {
    >         B() : a(*this) {}
    >         A a;
    >
    > };
    >
    > I just want this reference. No member access or similar (in the
    > construction process).
    > Allowed (and reasonable?) by standard or not?
    >
    > best regards,
    >         Alex


    This is perfectly safe and allowed in the standard.

    /Peter
    peter koch, Mar 18, 2008
    #2
    1. Advertising

  3. Alexander Stippler schrieb:
    > struct B;
    >
    > struct A
    > {
    > A(const B &_b) : b(_b) {}
    >
    > const B &b;
    > };
    >
    > struct B
    > {
    > B() : a(*this) {}
    > A a;
    > };
    >
    > I just want this reference. No member access or similar (in the
    > construction process).
    > Allowed (and reasonable?) by standard or not?


    This is OK in picipal. A typical parent child relation with object lifetime.
    But you *must* not dereference the pointer (or reference) before the
    contructor of B has completed. And also think about the case when B() or
    one of it's subfunctions throws and A's destructor is called.

    Marcel
    Marcel Müller, Mar 18, 2008
    #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. Razvan
    Replies:
    7
    Views:
    17,538
    Lee Fesperman
    Jul 4, 2004
  2. Aman
    Replies:
    4
    Views:
    422
    Agent Mulder
    Sep 15, 2003
  3. spipyeah

    initializer list question

    spipyeah, Oct 27, 2003, in forum: C++
    Replies:
    3
    Views:
    306
    spipyeah
    Oct 28, 2003
  4. The Directive

    Initializer List

    The Directive, Jan 17, 2004, in forum: C++
    Replies:
    8
    Views:
    618
    Ron Natalie
    Jan 19, 2004
  5. Alexander Stippler
    Replies:
    3
    Views:
    429
    Leor Zolman
    Apr 9, 2004
Loading...

Share This Page