c**********e 发帖数: 2007 | 1 如何对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?
} |
W*******e 发帖数: 1268 | |
c**********e 发帖数: 2007 | 3 C++. Here we need an assignment operator. The problem is that b.a is a const. How to assign a value to it?
【在 W*******e 的大作中提到】 : 常数一般从构造函数里面赋值吧,没看懂是啥语言
|
m*******p 发帖数: 141 | 4 Is it possible to implement the assigment operator for a class having a cons
t member data?
I guess no.
呼唤大牛来答疑。。。
const. How to assign a value to it?
【在 c**********e 的大作中提到】 : C++. Here we need an assignment operator. The problem is that b.a is a const. How to assign a value to it?
|
S**I 发帖数: 15689 | 5 no, const member can only be initialized
cons
【在 m*******p 的大作中提到】 : Is it possible to implement the assigment operator for a class having a cons : t member data? : I guess no. : 呼唤大牛来答疑。。。 : : const. How to assign a value to it?
|
c**********e 发帖数: 2007 | 6 So there is no assignment operator for a class with a const data member?
【在 S**I 的大作中提到】 : no, const member can only be initialized : : cons
|
S**I 发帖数: 15689 | 7 A b(a);
【在 c**********e 的大作中提到】 : 如何对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;
|
h**********l 发帖数: 6342 | 8 copy ctor应该可以的
【在 c**********e 的大作中提到】 : So there is no assignment operator for a class with a const data member?
|
S**I 发帖数: 15689 | 9 using constructor is initialization, not assignment.
【在 h**********l 的大作中提到】 : copy ctor应该可以的
|
c**********e 发帖数: 2007 | 10 Somebody provided such an answer. Does it make sense?
int *b=const_cast(&a.a;); |
|
|
S**I 发帖数: 15689 | 11 no, casting away constness of a const variable is undefined behavior.
【在 c**********e 的大作中提到】 : Somebody provided such an answer. Does it make sense? : int *b=const_cast(&a.a;);
|
b***i 发帖数: 3043 | 12 why do you need it? did you just think of this or there is an interview
question?
【在 c**********e 的大作中提到】 : 如何对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;
|
c**********e 发帖数: 2007 | 13 Yes. This was an interview question.
【在 b***i 的大作中提到】 : why do you need it? did you just think of this or there is an interview : question?
|
B******5 发帖数: 4676 | 14 如果const可以被assign不就违背本来的意愿了? |
g*******o 发帖数: 156 | 15 overload operator=, don't let compiler use the default one which is
memberwise assiangment.
In this operator= overloading, don't try to set constant variable. Then it
should be fine.
【在 c**********e 的大作中提到】 : 如何对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;
|
c**********e 发帖数: 2007 | 16 The the constant data member is not assigned.
【在 g*******o 的大作中提到】 : overload operator=, don't let compiler use the default one which is : memberwise assiangment. : In this operator= overloading, don't try to set constant variable. Then it : should be fine.
|
t****t 发帖数: 6806 | 17 if you want to assign it, why do you make it const in the first place?
【在 c**********e 的大作中提到】 : The the constant data member is not assigned.
|
g*******o 发帖数: 156 | 18 i didn't get what u really wanna last time, since it's kinda rare and not
that good practice in C++.
Here's my solution for you and i've tested it.
Like my last post, do the operator= overload. In operator=, define a int*pa=
(int*)&a; (or using reference).
Now we've escaped the compiler and can do assignmemt to const: *pa=xxx, xxx
may be member of object you passed from parameters at RHS of '='
Just like the other posts said, this is not a good way in C++, somehow
common in C...
【在 c**********e 的大作中提到】 : The the constant data member is not assigned.
|
c**********e 发帖数: 2007 | 19 This is a dammed interview question from somebody's interview experience.
【在 t****t 的大作中提到】 : if you want to assign it, why do you make it const in the first place?
|