Home Dashboard Directory Help
Search

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


Status: 

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)
view
0
User(s) can reproduce this bug

Description

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
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.