我只是在 Widget 中写了一个简单的 operator=,但是当我 operator= 它时,它会在 vs2010 中引发 Unhandled 异常。我在这些代码中没有看到任何错误。需要一些帮助。
class WidgetImpl
{
public:
WidgetImpl ( int _a, int _b, int _c ) :
a_ ( _a ), b_ ( _b ), c_ ( _c )
{
};
//WidgetImpl& operator= ( const WidgetImpl& rhs ) { //if I define this function,everything will be allright.
// if ( this == &rhs ) { return *this; }
////
////.... do something
////
//return *this;
// }
int a_, b_, c_;
std::vector<double> vector_;
};
class Widget
{
public:
Widget () : pImpl_ ( NULL ) {};
Widget ( const Widget& rhs ) {};
Widget& operator= ( const Widget& rhs )
{
if ( this == &rhs ) { return *this; }
*pImpl_ = * ( rhs.pImpl_ );
return ( *this );
}
void SetImp ( WidgetImpl* _Impl )
{
this->pImpl_ = _Impl;
}
WidgetImpl* pImpl_;
};
int main ( int argc, char** argv )
{
Widget w;
WidgetImpl* wimpl = new WidgetImpl ( 1, 2, 3 );
w.SetImp ( wimpl );
Widget w2;
w2 = w; //Unhandled exception throws here
return 0;
}
正如你在上面看到的。如果我在类 WidgetImpl 中定义 operator= 。似乎一切都好..奇怪..