Redian新闻
>
C++ Q48: illegal operation (C33)
avatar
C++ Q48: illegal operation (C33)# JobHunting - 待字闺中
c*e
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;
}
avatar
P*b
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;

avatar
s*t
3
只能是a了

【在 P*******b 的大作中提到】
: 这个是不是a?
avatar
x*y
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;

avatar
c*e
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...

avatar
P*b
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;

avatar
s*t
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;

avatar
x*y
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

avatar
s*t
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...

avatar
x*y
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
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。