c**********e 发帖数: 2007 | 1 Original source:
http://www.mitbbs.com/article_t/JobHunting/31348607.html
如何对const data member做assignment?
class A{
const int a;
public:
A():a(0){};
A(int m_a):a(m_a){};
};
int main(){
A a(1);
A b;
b = a; //how to implement assignment for this?
} |
h**k 发帖数: 3368 | 2 应该是系统自己创建一个缺省的copy constructor,做shalow copy,就是直接拷贝每
个member。 |
m*****n 发帖数: 2152 | 3 A b; b = a; 好像不是copy constructor吧,又不是 A b = a; overload operator "=
"吧.
A & A::operator =(const A &other)
{
if(this == &other)
return *this;
const_cast(a) = other.a;
return *this;
} |
c**********e 发帖数: 2007 | 4 This is good. Thank you.
"=
【在 m*****n 的大作中提到】 : A b; b = a; 好像不是copy constructor吧,又不是 A b = a; overload operator "= : "吧. : A & A::operator =(const A &other) : { : if(this == &other) : return *this; : const_cast(a) = other.a; : return *this; : }
|