473,322 Members | 1,408 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

initializer list question...

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
Feb 23 '07 #1
3 2159
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 23 '07 #2
Alf P. Steinbach wrote:
>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.
Okay, that makes sense. Thanks,

--John Ratliff
Feb 23 '07 #3
John Ratliff wrote:
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)

--
Alan Johnson
Feb 23 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: spipyeah | last post by:
My compiler is known not to be very compliant. So I can't base myself on the fact that the following doesn't work to determine whether or not it is legal C++. In an initializer list, can you...
1
by: Chris K | last post by:
I am relatively new to C++ and hope that this question is relevant. I have spent some time at the local library and some time on dejanews, but have no decided to go ahead with my question, since...
4
by: jfindlay | last post by:
Is it possible to populate a container with an initializer list? I am not too particular about how hackish or ugly any suggestions might be, I'm simply interested in investigating possible...
6
by: Marty M | last post by:
The following causes the "invalid initializer" message during gcc compile time... char firstword = word(question,1); the "word" function is... char * word(char *phrase, int what) {...body...
1
by: Vincent RICHOMME | last post by:
Hi, Let' say I have a class with an object pointer as member something like: // classA.h class A { A(B* pB); B* m_pB; };
2
by: cody | last post by:
class Test { IList list = new ArrayList(); MyCollection list2 = new MyCollection (list); } Leads to this error. I know I could initialize them in the ctor but I'm asking myself where this...
3
by: Ham Pastrami | last post by:
class Point { public: const int x, y; Point(int x, int y); } Point::Point(int x, int y) : x(x), y(y) { }
4
by: Ham Pastrami | last post by:
How do you initialize objects in the initializer list? Also, while this code is probably incorrect, it does compile (and execute) and I wonder what the actual result is. class Control { Point...
5
by: Pallav singh | last post by:
How can we justify that initializer list is better in performance than assignment list in constructor of C++ ??
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.