考虑以下代码:
class PrivateData
{
public:
PrivateData() : m_attr(0) {}
PrivateData& operator=( const PrivateData& data )
{
if ( &data != this )
{
m_attr = data.m_attr;
}
return *this;
}
private:
int m_attr;
};
class MyClass
{
public:
MyClass() : m_data( new PrivateData() ) {}
~MyClass() { delete m_data; }
MyClass& operator=( const MyClass& data )
{
if ( &data != this )
{
(*m_data) = data.privateData();
}
return *this;
}
inline const PrivateData& privateData() const { return *m_data; }
private:
PrivateData* m_data;
};
为什么cppcheck报告我?
成员变量'MyClass :: m_data'未在'MyClass :: operator ='中分配值。
在此处禁用警告是否安全?
注意:删除if ( &data != this )
无效。