L
lilburne
Julián Albo said:The output is:
a
Constructor
b
Copy constructor
No copy construction for a, and the copy constructor clearly works.
How do you tested in gcc 3.3.1?
Regards.
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
class Point {
public:
Point();
Point(const Point& p);
Point& operator=(const Point& p);
~Point();
};
Point:oint()
{
cout << "Point construction" << endl;
}
Point:oint(const Point& p)
{
cout << "Point copy construction" << endl;
}
Point::~Point()
{
cout << "Point destructor" << endl;
}
Point& Point:perator=(const Point& p)
{
cout << "Point assignment --- XXXX" << endl;
}
std::vector< Point > create()
{
// populate ...
std::vector< Point > nrv;
Point p;
nrv.push_back(p);
nrv.push_back(p);
return nrv;
}
void f(std::vector< Point > & vec) {
}
int main()
{
cout << "Create start" << endl;
std::vector< Point > res( create() );
cout << "Create finished" << endl;
f(res);
cout << "Create start" << endl;
res = create();
cout << "Create finished" << endl;
f(res);
return 0;
}
Output is:
Create start
Point construction
Point copy construction
Point copy construction
Point copy construction
Point destructor
Point destructor
Create finished
Create start
Point construction
Point copy construction
Point copy construction
Point copy construction
Point destructor
Point destructor
Point assignment --- XXXX
Point assignment --- XXXX
Point destructor
Point destructor
Create finished
Point destructor
Point destructor
As you can see the second call to create() results in the
copying (assignment). The NVRO optimization is minimal and I
can get the same effect as an earlier contributor said by
using the idiom:
vector < Point > vec;
create(vector< Point >& vec);
which isn't compiler dependent.