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 |
|