Wednesday, August 19, 2009

c++: const A a1; A a2(a1);--->error

Bloomberg LP » C++
Anonymous on January 23, 2009 | Question #68741 (Report Dup) | Edit | History

class A{
public:A(){}
A(A &a){}
A& operator=(constA& a)
{return(*this)}
};
int main()
{
const A a1;
A a2(a1);
return 0;
}

Given the sample code shown above, which one of the following statements is true?
The instantiation of a2 is illegal.
The constructor declaration for A is illegal.
There is nothing wrong with the code.
The assignment operator for A is illegal.
Class objects cannot be declared as const


Passing a const argument to a const reference: correct
Passing a non-const argument to a const reference: correct
Passing a non-const argument to a non-const reference: correct
Passing a const argument to a non-const reference: WRONG!!!

No comments:

Post a Comment