B
Brian C
Hello all,
I was playing around (don't know why, perhaps bored at work) with the
vector class. This was on an AIX machine using IBM's xlC compiler.
Anyway, I wrote a simple class, and inserted it a couple of times into
a vector. What I found odd was that when I inserted the 2nd item, I saw
that it called my copy constructor for the 1st item and the 2nd item.
When I inserted the 3rd object, it called the copy constructor for the
1st,2nd,3rd item, etc.
I got home and rewrote it quick and tested it under VC++, and it does
the same thing. Below is the code I ran on the VC++ compiler. I
understand the copy of the object I'm push_back()'ing, but why each item
already in the vector?
P.S. the code may not be pretty, just wrote it up quick for the test.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class DemoClass
{
public:
DemoClass() { cout << "default ctor" << endl; }
DemoClass(string Name) { Data=Name; cout << "ctor " << Data << endl; }
virtual ~DemoClass() { cout << "dtor " << Data << endl; }
DemoClass(const DemoClass &dc) { Data=dc.Data; cout << "copy ctor " <<
Data << endl; }
DemoClass &operator=(const DemoClass &dc) { Data=dc.Data; return(*this); }
private:
string Data;
};
int main(void)
{
vector<DemoClass> Vector;
DemoClass dc1("Hi");
DemoClass dc2("there");
DemoClass dc3("friend.");
cout << "Pushing 1..." << endl;
Vector.push_back(dc1);
cout << "Pushing 2..." << endl;
Vector.push_back(dc2);
cout << "Pushing 3..." << endl;
Vector.push_back(dc3);
cout << "Done" << endl;
}
I was playing around (don't know why, perhaps bored at work) with the
vector class. This was on an AIX machine using IBM's xlC compiler.
Anyway, I wrote a simple class, and inserted it a couple of times into
a vector. What I found odd was that when I inserted the 2nd item, I saw
that it called my copy constructor for the 1st item and the 2nd item.
When I inserted the 3rd object, it called the copy constructor for the
1st,2nd,3rd item, etc.
I got home and rewrote it quick and tested it under VC++, and it does
the same thing. Below is the code I ran on the VC++ compiler. I
understand the copy of the object I'm push_back()'ing, but why each item
already in the vector?
P.S. the code may not be pretty, just wrote it up quick for the test.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class DemoClass
{
public:
DemoClass() { cout << "default ctor" << endl; }
DemoClass(string Name) { Data=Name; cout << "ctor " << Data << endl; }
virtual ~DemoClass() { cout << "dtor " << Data << endl; }
DemoClass(const DemoClass &dc) { Data=dc.Data; cout << "copy ctor " <<
Data << endl; }
DemoClass &operator=(const DemoClass &dc) { Data=dc.Data; return(*this); }
private:
string Data;
};
int main(void)
{
vector<DemoClass> Vector;
DemoClass dc1("Hi");
DemoClass dc2("there");
DemoClass dc3("friend.");
cout << "Pushing 1..." << endl;
Vector.push_back(dc1);
cout << "Pushing 2..." << endl;
Vector.push_back(dc2);
cout << "Pushing 3..." << endl;
Vector.push_back(dc3);
cout << "Done" << endl;
}