Topic: warning: 'this' : used in base member init


Author: jkanze@otelo.ibmmail.com
Date: 1998/05/28
Raw View
In article <6khhp3$nhd$1@nnrp1.dejanews.com>,
  A.Stokes@globalcomm.co.uk wrote:
>
> In article <6k49v6$7hr@engnews1.Eng.Sun.COM>,
>   clamage@Eng wrote:
> >
> > In article 748A@lehman.com, Cristian Georgescu <cgeorges@lehman.COM>
writes:
>
> > >warning C4355: 'this' : used in base member initializer list
> > >
> > >Running the program, shows that it works. However, I don't understan=
d
> > >the warning, and if it has to do with the standard usage of "this"?
> >
> > The potential problem with using "this" in an member initializer is
> > that the recipient might expect that the object has been constructed,
> > when in fact it hasn't been. Example:
> ...
> > It's quite reasonable for the compiler to warn about passing "this"
> > to a base class, since you've opened the door to a bug that might
> > be hard to track down.
>
> Isn't this actually ill-formed, or possibly just undefined, and isn't t=
he
> diagnostic required? The FDIS defines the meaning of 'this' in the body=
 of
a
> nonstatic member function (9.3.2, [class.this]), but that doesn't inclu=
de a
> constructor member/base initialiser list.

Section 5.1/2: "The keyword this shall be used only inside a nonstatic
class member function body OR in a constructor mem-initializer."
(Emphisis added.)  The expression is well defined, although there are
only limited things you can do with the pointer.

(Regretfully, one of things you can't legally do is convert it to a
pointer to a base class, which means that in practice, there is no
way to have the streambuf derived class part of the iostream derived
object -- it must be allocated on the heap.  There goes Dietmar
Kuehl's clever trick out the window, even if I cannot imagine a
case where it wouldn't work.)

--
James Kanze    +33 (0)1 39 23 84 71    mailto: kanze@gabi-soft.fr
        +49 (0)69 66 45 33 10    mailto: jkanze@otelo.ibmmail.com
GABI Software, 22 rue Jacques-Lemercier, 78000 Versailles, France
Conseils en informatique orient=E9e objet --
              -- Beratung in objektorientierter Datenverarbeitung

-----=3D=3D Posted via Deja News, The Leader in Internet Discussion =3D=3D=
-----
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              ]






Author: clamage@Eng (Steve Clamage)
Date: 1998/05/22
Raw View
In article 748A@lehman.com, Cristian Georgescu <cgeorges@lehman.COM> writes:
>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"?

The potential problem with using "this" in an member initializer is
that the recipient might expect that the object has been constructed,
when in fact it hasn't been. Example:

class Der;
void f(Der*);

class Base {
public:
 Base(Der* d) { f(d); }
};

class Der : public Base {
public:
 Der() : Base(this) { ... }
};

Function f gets passed a Der*, and would normally expect that
the Der object has been constructed. In this case, the object
isn't even a Der yet -- it's a Base!

It's quite reasonable for the compiler to warn about passing "this"
to a base class, since you've opened the door to a bug that might
be hard to track down.

Your simplified example is less problematic, but the compiler is
warning you so that you realize you must be careful about using
"ptr" in the constructor for A.

---
Steve Clamage, stephen.clamage@sun.com




[ 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: A.Stokes@globalcomm.co.uk
Date: 1998/05/27
Raw View
In article <6k49v6$7hr@engnews1.Eng.Sun.COM>,
  clamage@Eng wrote:
>
> In article 748A@lehman.com, Cristian Georgescu <cgeorges@lehman.COM> writes:

> >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"?
>
> The potential problem with using "this" in an member initializer is
> that the recipient might expect that the object has been constructed,
> when in fact it hasn't been. Example:
...
> It's quite reasonable for the compiler to warn about passing "this"
> to a base class, since you've opened the door to a bug that might
> be hard to track down.

Isn't this actually ill-formed, or possibly just undefined, and isn't the
diagnostic required? The FDIS defines the meaning of 'this' in the body of a
nonstatic member function (9.3.2, [class.this]), but that doesn't include a
constructor member/base initialiser list.

There is a note in 12.6.2, [class.base.init], para 7 saying that because
member initialisers are looked up in the constructor scope you can use 'this'
- but notes aren't normative, and 'this' is a keyword with a defined meaning,
not a name looked up in a scope.

- Alan

-----== 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              ]