求教stl中map的对象赋值过程(无operator=定义却能正常赋值)
这几天看对象模型,结合手头上做的一个程序,有需要结构用到map的赋值,如下面代码:
class NODE{public: int state; int id; unsigned long RouteTable[2][32]; map<unsigned long, unsigned long> mapMM;};NODE n1, n2;n1.RouteTable[0][0] = 1;n1.mapMM.insert( map<unsigned long, unsigned long>::value_type( 1,1 ) );n1.mapMM.insert( map<unsigned long, unsigned long>::value_type( 2,2 ) );n2 = n1; //进行整个对象赋值n2.RouteTable[0][0] = 2;n2.mapMM.insert( map<unsigned long, unsigned long>::value_type( 3,3 ) );
class NODE{public: int state; int id; unsigned long RouteTable[2][32]; map<unsigned long, unsigned long> mapMM; NODE& operator=( NODE& rhs ) { if( this == &rhs ) return *this; id= rhs.id; state = rhs.state; mapMM.insert( rhs.mapMM.begin(), rhs.mapMM.end() ); //是不是多余了呢? return *this; } };class A{ A(const A&) { cout << "A copy construct"<<endl; } A& operator=(const A& a) { cout <<"A operator= called"<<endl; } .........};A a;A b=a;//拷贝构造A c;c=a;//等号重载int a1;int b1=a1;int c1;c1=a1;