练习13.4
class Point {
public:
Point() :_x(0), _y(0) { cout << "Point()" << endl; }
Point(const Point &rhs)
:_x(rhs._x), _y(rhs._y)
{ cout << "Point(const &)" << endl; }
void print() {
cout << _x << " " << _y << endl;
}
private:
int _x;
int _y;
};
Point global;
Point foo_bar(Point arg) {
Point local = arg;
Point *heap = new Point(global);
*heap = local;
Point pa[4] = { local, *heap };
delete heap;
return pa[0];
}
int main() {
Point point;
Point p = foo_bar(point);
p.print();
return 0;
}
- 编译添加参数-fno-elide-constructor,输出
Point()
Point()
Point(const &)
Point(const &)
Point(const &)
Point(const &)
Point(const &)
Point()
Point()
Point(const &)
Point(const &)
练习13.56
class Foo {
public:
Foo sorted() &&;
Foo sorted() const &;
private:
vector<int> data;
};
Foo Foo::sorted() && {
sort(data.begin(), data.end());
return *this;
}
Foo Foo::sorted() const & {
Foo ret(*this);
cout << "sorted" << endl;
return ret.sorted();
}
int main() {
Foo f;
f.sorted();
return 0;
}
练习13.57
class Foo {
public:
Foo sorted() &&;
Foo sorted() const &;
private:
vector<int> data;
};
Foo Foo::sorted() && {
cout << "sorted &&" << endl;
sort(data.begin(), data.end());
return *this;
}
Foo Foo::sorted() const & {
return Foo(*this).sorted();
}
int main() {
Foo f;
f.sorted();
return 0;
}