64,681
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <vector>
using namespace std;
class TestA
{
public:
TestA()
{
cout << "Create TestA" << endl;
}
~TestA()
{
cout << "Destory TestA" << endl;
}
};
int main()
{
vector<TestA> mytest;
mytest.resize(4);
mytest.resize(3);
return 0;
}
int main()
{
vector<TestA> mytest;
printf("%d %d\n", mytest.capacity(), mytest.size());
mytest.resize(4);
printf("%d %d\n", mytest.capacity(), mytest.size());
mytest.resize(3);
printf("%d %d\n", mytest.capacity(), mytest.size());
return 0;
}
[/quote]
soga,只有第一次是调用默认构造函数,然后后面都是调用拷贝构造函数,谢谢
int main()
{
vector<TestA> mytest;
printf("%d %d\n", mytest.capacity(), mytest.size());
mytest.resize(4);
printf("%d %d\n", mytest.capacity(), mytest.size());
mytest.resize(3);
printf("%d %d\n", mytest.capacity(), mytest.size());
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
class TestA
{
public:
TestA()
{
cout << "Create TestA1" << endl;
}
TestA(const TestA & a)
{
cout << "Create TestA2" << endl;
}
~TestA()
{
cout << "Destory TestA" << endl;
}
};
int main()
{
vector<TestA> mytest;
printf("%d\n", mytest.capacity());
mytest.resize(4);
printf("%d\n", mytest.capacity());
mytest.resize(3);
printf("%d\n", mytest.capacity());
return 0;
}
执行结果:
0
Create TestA1
Create TestA2
Create TestA2
Create TestA2
Create TestA2
Destory TestA
4
Create TestA1
Destory TestA
Destory TestA
4
Destory TestA
Destory TestA
Destory TestA
请按任意键继续. . .
#include <iostream>
#include <vector>
using namespace std;
class TestA
{
public:
TestA()
{
cout << "Create TestA1" << endl;
}
TestA(const TestA & a)
{
cout << "Create TestA2" << endl;
}
~TestA()
{
cout << "Destory TestA" << endl;
}
};
int main()
{
vector<TestA> mytest;
mytest.resize(4);
mytest.resize(3);
return 0;
}