seems you forget about copy ctors.
You are right, the behaviour of vector is implementation defined. That's the gcc 3.4.2 output:
ctor 0
copy ctor 1
copy ctor 2
copy ctor 3
dtor 4
ctor 5
copy opt 6
dtor 7
dtor 8
dtor 9
dtor 10
#include <vector>
#include <iostream>
class Test
{
static int _no;
public:
Test()
{
std::cout << "ctor " << _no++ << std::endl;
}
Test(const Test&)
{
std::cout << "copy ctor " << _no++ << std::endl;
}
~Test()
{
std::cout << "dtor " << _no++ << std::endl;
}
Test& operator= (const Test&)
{
std::cout << "copy opt " << _no++ << std::endl;
}
};
int Test::_no = 0;
int main()
{
std::vector<Test> x;
x.resize(3);
x[ 15 ] = Test();
return 0;
}
#include <vector>
#include <iostream>
class Test
{
static int _no;
public:
Test()
{
std::cout << "ctor " << _no++ << std::endl;
}
Test(const Test&)
{
std::cout << "copy ctor " << _no++ << std::endl;
}
~Test()
{
std::cout << "dtor " << _no++ << std::endl;
}
Test& operator= (const Test&)
{
std::cout << "copy opt " << _no++ << std::endl;
}
};
int Test::_no = 0;
int main()
{
std::vector<Test> x;
x.resize(3);
x[ 15 ] = Test();
return 0;
}
To copy to clipboard, switch view to plain text mode
Bookmarks