由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - C++ Q61: 如何对const data member做assignment?
相关主题
C++: Q75 copy constructor 问什么用 const reference?BB phone interview question
请教一个C++问题请问这个C++问题对吗?
问几个跟C++有关的面试题C++ Q47: protected constructor (C39)
Please help on Effective C++ Item 11copy constructor 的问题
发发我自己的Bloomberg的面经Qualcomm Phone Interbview面筋,赚RP
One C++ question贴一道take home的面试题
c++ class default functions?今早Bloomberg电话面试经过,奉献给大家。。。
还是基础题新Qualcomm面经
相关话题的讨论汇总
话题: const话题: assignment话题: int话题: member话题: q61
进入JobHunting版参与讨论
1 (共1页)
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;
: }

1 (共1页)
进入JobHunting版参与讨论
相关主题
新Qualcomm面经发发我自己的Bloomberg的面经
再帖一遍Amazon Onsite的题One C++ question
Bloomberg网上测试题c++ class default functions?
包子呼唤大牛--问关于C++Destructor的问题 (转载)还是基础题
C++: Q75 copy constructor 问什么用 const reference?BB phone interview question
请教一个C++问题请问这个C++问题对吗?
问几个跟C++有关的面试题C++ Q47: protected constructor (C39)
Please help on Effective C++ Item 11copy constructor 的问题
相关话题的讨论汇总
话题: const话题: assignment话题: int话题: member话题: q61