Consider this class with three constructors:
class Circle {
public:
Circle(int r) {
_radius = r;
}
Circle(const Circle& c){
_radius = c.radius();
cout << endl << "Copy constructor with lvalue reference. Radius: " << _radius;
}
Circle(Circle&& c){
_radius = c.radius();
cout << endl << "Copy constructor with rvalue reference. Radius:" << _radius;
}
int radius() const {
return _radius;
}
private:
int _radius;
};
int main() {
Circle c1(2);
Circle c2(c1);
cout << endl << c2.radius();
Circle c3(Circle(4));
cout << endl << c3.radius();
return 0;
}
“g++-std=c++0x” 产出如下:
Copy constructor with lvalue reference. Radius: 2
2
4
OK. The right constructors for the first two cases are called. But for the third case i.e., Circle c3(Circle(4)), I d expect the third constructor, (copy constructor with rvalue referecne) to be called but it s not the case. Obviously some constructor is called since c3 is properly instantiated but I don t understand why the compiler is not using the explicitly provided one. Am I missing something here?