I have a Visual Studio 2008 C++ application where I’ve created a custom streambuf and stream implementation. I’ve used an approach given by Angelika Langer for creating the stream detailed below:
class MyOutputStream_3 : private MyStreamBuf, public ostream {
public:
MyOutputStream_3() : ostream((MyStreamBuf*) this), ios(0) {};
virtual ~MyOutputStream_3() { sync(); }
// ...
};
But, Visual Studio gives me a warning:
warning C4355: 'this' : used in base member initializer list
The code works fine, but I’m worried the compiler is informing me that what I’ve done either could break under some circumstances or may be non-portable.
Is this something I can ignore in this instance or what should I do to fix my issue?
The C++03 standard has this bit in a note for 12.6.2/7 “Initializaing bases and members”:
I think the warning is issued becuase the object being referred to by the
thispointer isn’t fully initialized so there’s some potential danger. As long as your base doesn’t actually use the uninitialized parts of the object until after they’re initialized, you should be fine.As the MSDN docs for the warning (http://msdn.microsoft.com/en-us/library/3c594ae3.aspx) mention: