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

Status : 

  Fixed<br /><br />
		This item has been fixed in the current or upcoming version of this product.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.

Sign in
to vote
ID 718050 Comments
Status Closed Workarounds
Type Bug Repros 0
Opened 1/11/2012 10:51:02 AM
Access Restriction Public


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.

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.

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)