initializer list question...

J

John Ratliff

I've been using g++ for a long time (and this is not a g++ question),
and I recently started using MSVC Express. Some of my programs that I
rebuilt with MSVC give me a warning I don't understand.

...\..\source\view\Frame.cc(46) : warning C4355: 'this' : used in base
member initializer list

The offending line:

Frame::Frame() : timer(this, ID_SBTIMER), sram(NULL),
ignoreTextEvents(true) {

Is it bad to use 'this' here? I've never had a problem with the binary
compiled either by g++ or MSVC, but is it wrong or dangerous in some way?

Thanks,

--John Ratliff
 
A

Alf P. Steinbach

* John Ratliff:
I've been using g++ for a long time (and this is not a g++ question),
and I recently started using MSVC Express. Some of my programs that I
rebuilt with MSVC give me a warning I don't understand.

..\..\source\view\Frame.cc(46) : warning C4355: 'this' : used in base
member initializer list

The offending line:

Frame::Frame() : timer(this, ID_SBTIMER), sram(NULL),
ignoreTextEvents(true) {

Is it bad to use 'this' here?

No, but see below.

I've never had a problem with the binary
compiled either by g++ or MSVC, but is it wrong or dangerous in some way?

If the 'timer' object calls back on the 'Frame' object before the
'Frame' object constructor has finished successfully (and initialized
everything), bad things can happen.
 
J

John Ratliff

Alf said:
If the 'timer' object calls back on the 'Frame' object before the
'Frame' object constructor has finished successfully (and initialized
everything), bad things can happen.

Okay, that makes sense. Thanks,

--John Ratliff
 
A

Alan Johnson

John said:
I've been using g++ for a long time (and this is not a g++ question),
and I recently started using MSVC Express. Some of my programs that I
rebuilt with MSVC give me a warning I don't understand.

..\..\source\view\Frame.cc(46) : warning C4355: 'this' : used in base
member initializer list

The offending line:

Frame::Frame() : timer(this, ID_SBTIMER), sram(NULL),
ignoreTextEvents(true) {

Is it bad to use 'this' here? I've never had a problem with the binary
compiled either by g++ or MSVC, but is it wrong or dangerous in some way?

Thanks,

--John Ratliff

You can easily get undefined behavior passing 'this' to something in an
initializer list. Consider the following example to see why:

struct A
{
int x ;
} ;

struct C ;

struct B
{

B(C * p) ;
} ;

struct C
{
B b ;
A a ;

C() ;
} ;

B::B(C * p)
{
// p->a hasn't been constructed yet! Undefined behavior.
p->a.x = 42 ;
}

C::C() : b(this)
{}


Of course, this is just a warning. If you are sure you don't have any
undefined behavior, then you can ignore it or disable it. I think in
VC++ you would disable it with something like:
#pragma warning(disable:4355)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,270
Latest member
TopCryptoTwitterChannels_

Latest Threads

Top