Search

Warning C4355: 'this' : used in base member initializer list by Uray M. János

Closed
as Fixed Help for as Fixed

1
2
Sign in
to vote
Type: Bug
ID: 718050
Opened: 1/11/2012 10:51:02 AM
Access Restriction: Public
Moderator Decision: Sent to Engineering Team for consideration
0
Workaround(s)
0
User(s) can reproduce this bug
MSDN says (in http://msdn.microsoft.com/en-us/library/3c594ae3.aspx ), that "this" cannot be used in the constructor's initializer list, because "you've passed a pointer to an unconstructed object to another constructor". It is ok.
But consider my example below.
The compiler generates (two) C4355 warnings, and this is wrong, because "this" here refers only to the base class subobject "Base", because X::X() takes a "Base*". And the "Base" subobject is fully constructed.
Not even an explicit cast prevents this warning, and I have no idea, how to prevent this warning in the code -- given that X has this constructor and Derived has a data member of type X.
Details (expand)

Visual Studio/Silverlight/Tooling version

Visual Studio 2010

What category (if any) best represents this feedback?

 

Steps to reproduce

// cl /c new.cpp

struct Base {
};

struct X {
X (Base*);
};

struct Derived: Base {
X x;
Derived (): x(this) {} // warning C4355
Derived (int): x(static_cast<Base*>(this)) {} // warning C4355, despite of the cast
};

Product Language

English

Operating System

Windows XP

Operating System Language

English

Actual results

new.cpp(10) : warning C4355: 'this' : used in base member initializer list
new.cpp(11) : warning C4355: 'this' : used in base member initializer list

Expected results

The compiler should either:
* check whether "this" refers to the object being constructed or to an already constructed base class subobject, and print the warning only in the first case;
* or disable this warning by default.
File Attachments
0 attachments
Sign in to post a comment.
Posted by Microsoft on 1/27/2012 at 7:38 AM
Hi: we changed the compiler so that this warning is now off-by-default.

Thanks
Jonathan Caves
Visual C++ Compiler Team
Posted by MS-Moderator10 [Feedback Moderator] on 1/11/2012 at 9:24 PM
Thank you for submitting feedback on Visual Studio 2010 and .NET Framework. Your issue has been routed to the appropriate VS development team for investigation. We will contact you if we require any additional information.
Posted by Cassio Neri on 1/11/2012 at 3:19 PM
I believe the warning is right. In Derived's initializer list "this" is of type Derived* and, thus, the object is still unconstructed.

Even with the cast (automatic or explict static_cast) the situation is still potentially dangerous. Indeed, if Base were polymorphic (had any virtual method), then X::X(Base* p) could do

Derived* q = dynamic_cast<Derived*>(p);

which would succeed (q wouldn't be NULL) and produce a pointer to an uninitialized object. Unfortunately, there's no way to the compiler to warn you at this exact point. The best it can do is exactly what it's doing.

If you are sure you're doing nothing wrong with "this", then you can locally disable the warning with a #pragma just before Derived's constructors:

#pragma warning(disable: 4355)
Posted by MS-Moderator01 on 1/11/2012 at 11:43 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)
Sign in to post a workaround.