由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - C++: 如何对const data member做assignment?
相关主题
请问一道c++题目Bloomberg phone interview (intern)
请教operator const char*() 的问题C++ question
C++ Q48: illegal operation (C33)请问如何准备C/C++面试
C++ 面试题[合集] 贡献几个C/C++编程电话面试题
C++ Q55: (C17)C++ Q71: overloading (skillport)
请问关于overloading << (转载)C++ Q89: function template and overloading
请帮忙看道题 c++ operator overload问一个c++问题关于reference vs. pointer
问个c++题如何准备bloomberg online test
相关话题的讨论汇总
话题: const话题: assignment话题: member话题: int话题: c++
进入JobHunting版参与讨论
1 (共1页)
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
2
常数一般从构造函数里面赋值吧,没看懂是啥语言
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;);
相关主题
请问关于overloading << (转载)Bloomberg phone interview (intern)
请帮忙看道题 c++ operator overloadC++ question
问个c++题请问如何准备C/C++面试
进入JobHunting版参与讨论
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?
1 (共1页)
进入JobHunting版参与讨论
相关主题
如何准备bloomberg online testC++ Q55: (C17)
static initialization dependency c++ (转载)请问关于overloading << (转载)
Can you overload the same operator more than once?请帮忙看道题 c++ operator overload
Tree Iterator && operator overloading的一个问题问个c++题
请问一道c++题目Bloomberg phone interview (intern)
请教operator const char*() 的问题C++ question
C++ Q48: illegal operation (C33)请问如何准备C/C++面试
C++ 面试题[合集] 贡献几个C/C++编程电话面试题
相关话题的讨论汇总
话题: const话题: assignment话题: member话题: int话题: c++