Suppose I have:
// MyClass.h
class MyClass
{
public:
MyClass();
private:
Something *something_;
}
// MyClass.cpp
MyClass::MyClass()
{
something_ = new Something();
}
Should I initialize something_ to NULL (or 0) in the constructor initialization list of the MyClass constructor? Or is that not necessary because I'm assigning to it in the body of the constructor? What is the recommended practice?
MyClass::MyClass() : something_(new Something())
– Ornithorhynchusshared_ptr
orscoped_ptr
instead of a native pointer so that cleanup is automatic, even in the case of exceptions. – Ornithorhynchus