Topic: warning: 'this' : used in base member initializer list ?


Author: Cristian Georgescu <cgeorges@lehman.COM>
Date: 1998/05/22
Raw View
I am trying to use "this" in the initialization list, for initializing
an attribute of the class. A simplified code sample (the real example
is  more complicated) would be:

class A
{
public:

    A() : ptr(this) {}

    A*const ptr;
};

The compiler sends a warning like:

warning C4355: 'this' : used in base member initializer list

Running the program, shows that it works. However, I don't understand
the warning, and if it has to do with the standard usage of "this"?

--
                      Cristian Georgescu
_________________________________________________
                      email: CGeorges@lehman.com
                      tel:   (212) 526-3502

    _/      _/_/_/_/  Lehman Brothers
   _/      _/    _/   Equity Finance Systems
  _/      _/_/_/_/    World Financial Center
 _/      _/    _/     200 Vesey Street
_/_/_/  _/_/_/_/      New York, NY 10285.
_________________________________________________



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Pete Becker <petebecker@acm.org>
Date: 1998/05/25
Raw View
Cristian Georgescu wrote:
>
> I am trying to use "this" in the initialization list, for initializing
> an attribute of the class. A simplified code sample (the real example
> is  more complicated) would be:
>
> class A
> {
> public:
>
>     A() : ptr(this) {}
>
>     A*const ptr;
> };
>
> The compiler sends a warning like:
>
> warning C4355: 'this' : used in base member initializer list
>
> Running the program, shows that it works. However, I don't understand
> the warning, and if it has to do with the standard usage of "this"?

At the point where 'this' is being used in the code the object under
construction has not been fully constructed. The compiler is warning you
about that. In many cases (including yours) it's perfecly safe to use
'this'. But if you passed it to a function that called member functions
you could easily get into trouble.
 -- Pete
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Brian Matthews <brian_matthews@ie.ibm.com>
Date: 1998/05/25
Raw View
The reason it is a warning is because the class pointed to by 'this'
has not been fully constructed and you are passing it to another
constructer. This isn't a problem because the constructor in question
is very a trivial copy of the pointer value.

But consider the following example:

class A;

class B
{
public:
    B(A* p);
};

class A
{
public:
    A() : b(this) { m = 1234; }

    B b;
    int m;
};


B::B(A* p) { cout << p->m << end; }

What appears on the screen? Well it won't be 1234! Because 'p' had not
been fully constructed and the value of 'p->m' is undefined in the 'b'
constructor.

Brian Matthews (brian_matthews@ie.ibm.com)

Cristian Georgescu wrote:

> I am trying to use "this" in the initialization list, for initializing
> an attribute of the class. A simplified code sample (the real example
> is  more complicated) would be:
>
> class A
> {
> public:
>
>     A() : ptr(this) {}
>
>     A*const ptr;
> };
>
> The compiler sends a warning like:
>
> warning C4355: 'this' : used in base member initializer list
>
> Running the program, shows that it works. However, I don't understand
> the warning, and if it has to do with the standard usage of "this"?
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: jkanze@otelo.ibmmail.com
Date: 1998/05/26
Raw View
In article <3569A149.CBB11E12@ie.ibm.com>,
  Brian Matthews <brian_matthews@ie.ibm.com> wrote:
>
> The reason it is a warning is because the class pointed to by 'this'
> has not been fully constructed and you are passing it to another
> constructer.

There are admittedly cases, like the one you show, where this might
be dangerous.  On the other hand, it is often useful, and the compiler
in question (VC++) generates the warning even in the case where the
this is being used as a pointer to an already constructed base, e.g.:

    struct B{} ;
    struct C { C( B* ) ; } ;
    struct D : B , C
    {
        D() : C( this ) {}
    }

(Warning: I've not checked the exact example above, but I have had
the warning from similar cases, where a second base class expected
a pointer to the first base class, and was passed this.)

> Cristian Georgescu wrote:
>
> > I am trying to use "this" in the initialization list, for initializing
> > an attribute of the class. A simplified code sample (the real example
> > is  more complicated) would be:
> >
> > class A
> > {
> > public:
> >
> >     A() : ptr(this) {}
> >
> >     A*const ptr;
> > };
> >
> > The compiler sends a warning like:
> >
> > warning C4355: 'this' : used in base member initializer list
> >
> > Running the program, shows that it works. However, I don't understand
> > the warning, and if it has to do with the standard usage of "this"?

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/   Now offering spam-free web-based newsreading
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]