由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - C++ Q48: illegal operation (C33)
相关主题
c++ new的一个问题请教个C++编程思路
分享面经 mathworks为什么C++的constructor出错可以抛出异常,而destructor出错
bloomberg 问题: C++ construct 时用 new 没"()"什么情况下pass by reference比pass by pointer好?
A problem about Heap and Stack.A malloc/free question using C/C++
关于multithread programming大家看什么书Interview Questions
面试题: Multithreads 之间怎么通信?求救:Bloomberg的面试
面經面經关于Bloomberg Phone Interview
Char x[] = "abc"; 是在heap还是stack上? (转载)请教几个面试问题
相关话题的讨论汇总
话题: const话题: operator话题: reference话题: rhs话题: static
进入JobHunting版参与讨论
1 (共1页)
c**********e
发帖数: 2007
1
If a class has + and = operator overloaded, and three objects a, b, c
constructed, which of the following is illegal?
a) (c=a+a)=b+c;
b) a=a=b+c;
More detail:
____________________
class X {
public:
X& operator=(const X& rhs);
const X& operator+(const X& rhs) const;
private;
int n;
};
int main() {
X a, b, c;
// Statement goes here
return 0;
}
P*******b
发帖数: 1001
2
这个是不是a?

【在 c**********e 的大作中提到】
: If a class has + and = operator overloaded, and three objects a, b, c
: constructed, which of the following is illegal?
: a) (c=a+a)=b+c;
: b) a=a=b+c;
: More detail:
: ____________________
: class X {
: public:
: X& operator=(const X& rhs);
: const X& operator+(const X& rhs) const;

s*********t
发帖数: 1663
3
只能是a了

【在 P*******b 的大作中提到】
: 这个是不是a?
x***y
发帖数: 633
4
It really depends on how you overload these operators...If you follow the
convention of built in types and types in standard libraries ( return a non-
const reference to *this) a) actually works; but for b), the self-assignment
can often lead to mistake for badlly overloaded = operator...

【在 c**********e 的大作中提到】
: If a class has + and = operator overloaded, and three objects a, b, c
: constructed, which of the following is illegal?
: a) (c=a+a)=b+c;
: b) a=a=b+c;
: More detail:
: ____________________
: class X {
: public:
: X& operator=(const X& rhs);
: const X& operator+(const X& rhs) const;

c**********e
发帖数: 2007
5
More detail is given -- I was lazy in typing the code -- now I have done it.

non-
assignment

【在 x***y 的大作中提到】
: It really depends on how you overload these operators...If you follow the
: convention of built in types and types in standard libraries ( return a non-
: const reference to *this) a) actually works; but for b), the self-assignment
: can often lead to mistake for badlly overloaded = operator...

P*******b
发帖数: 1001
6
题不全吧,都是对的。

【在 c**********e 的大作中提到】
: If a class has + and = operator overloaded, and three objects a, b, c
: constructed, which of the following is illegal?
: a) (c=a+a)=b+c;
: b) a=a=b+c;
: More detail:
: ____________________
: class X {
: public:
: X& operator=(const X& rhs);
: const X& operator+(const X& rhs) const;

s*****t
发帖数: 737
7
似乎没有答案啊。

【在 c**********e 的大作中提到】
: If a class has + and = operator overloaded, and three objects a, b, c
: constructed, which of the following is illegal?
: a) (c=a+a)=b+c;
: b) a=a=b+c;
: More detail:
: ____________________
: class X {
: public:
: X& operator=(const X& rhs);
: const X& operator+(const X& rhs) const;

x***y
发帖数: 633
8
Then, I think the answer is a.
The problem is this declaration:
const X& operator+(const X& rhs) const;
then in a)
a+a is a.operator+(a) and notice a reference is returned。
we know that we can not return a reference to a local stack, a local static,
or a heap-allocated object, so the only possibility is that a itself is
returned. But as the + operation, we return the sum, in which the a's value
must be changed, which voilate the signature of pass by reference to const
and const function...

it.

【在 c**********e 的大作中提到】
: More detail is given -- I was lazy in typing the code -- now I have done it.
:
: non-
: assignment

s*****t
发帖数: 737
9
It seems to me that it depends on how operator+ is defined.
We can return a reference to a heap-allocated object and a local static.
Actually, we sometimes need to return a reference to a local static,
due to initialization order problem.
We can create a heap-allocated object, so it is not clear to me that a's
value has to be changed.

static,
value

【在 x***y 的大作中提到】
: Then, I think the answer is a.
: The problem is this declaration:
: const X& operator+(const X& rhs) const;
: then in a)
: a+a is a.operator+(a) and notice a reference is returned。
: we know that we can not return a reference to a local stack, a local static,
: or a heap-allocated object, so the only possibility is that a itself is
: returned. But as the + operation, we return the sum, in which the a's value
: must be changed, which voilate the signature of pass by reference to const
: and const function...

x***y
发帖数: 633
10
returning a reference to a heap-allocated in this one is a bad idea, as
there is no way for you to delete it; about reference to a local static
object, your statement is right, local static works, but it's weired to use
it here, especially it will create problems for multithreaded application
when + is such a freqently used operator.
Without definition of operators, the answer is that it depends. However, in
my opinion, from pratical perspective, either time overhead is very high for
synchroniz
1 (共1页)
进入JobHunting版参与讨论
相关主题
请教几个面试问题关于multithread programming大家看什么书
请推荐Multithread的学习资料。面试题: Multithreads 之间怎么通信?
紧急求助!!!面經面經
说说flextrade 的 onsite 经历Char x[] = "abc"; 是在heap还是stack上? (转载)
c++ new的一个问题请教个C++编程思路
分享面经 mathworks为什么C++的constructor出错可以抛出异常,而destructor出错
bloomberg 问题: C++ construct 时用 new 没"()"什么情况下pass by reference比pass by pointer好?
A problem about Heap and Stack.A malloc/free question using C/C++
相关话题的讨论汇总
话题: const话题: operator话题: reference话题: rhs话题: static