warning: 'this' used in base member initializer list

From: Derek (user_at_nospam.org)
Date: 12/22/04


Date: Wed, 22 Dec 2004 16:05:19 -0500

I get the following warning

   warning C4355: 'this' : used in base member initializer list

when I compile the following program with VC6 and VC7 (the latest
GCC and Comeau doesn't think anything is amiss):

   class Y;

   class X {
   public:
       X(Y& y) : m_y(y) {}
   private:
       Y& m_y;
   };

   class Y {
   public:
       Y() : m_x(*this) {} // <<<<< warning here
   private:
       X m_x;
   };

   int main() {
     Y y;
     return 0;
   }

I'm guessing the warning stems from the fact that I'm passing
the 'this' pointer of a not-yet-fully-constructed object to a
function (X's ctor).

However, I'm not actually using 'this', just passing and storing
it (albeit via reference). Is this safe provided that I don't
try to use X::m_y until the object it refers to is constructed?

Thanks.



Relevant Pages