在没有明确拷贝构造函数的时候
#include <stdarg.h>
#include <stdio.h>
#include <stdint.h>
#include <iostream>
#include <vector>
int count = 1;
class temptest
{
public:
temptest();
/*temptest(temptest const& other)
{
test = count;
printf("copy be build %d\n", test);
count ++;
}*/
~temptest();
int check();
private:
uint8_t check_ = 255;
uint8_t test;
};
temptest::temptest(/* args */)
{
test = count;
printf("be build %d\n", test);
count ++;
}
temptest::~temptest()
{
printf("be destory %d\n", test);
}
int temptest::check()
{
uint8_t test_temp = (~test) + 1;
if(test_temp == check_)
{
printf("okok:%d, %d, %d\n", test_temp, test, check_);
return 1;
}
else
{
printf("nonono:%d, %d, %d\n", test_temp, test, check_);
return 0;
}
}
temptest getInst()
{
return temptest();
}
int main()
{
std::vector<temptest> ptr(5);
printf("\n//\n");
ptr.push_back(getInst());
printf("\n//\n");
for(temptest test : ptr)
{
printf("delete\n");
}
printf("\n//\n");
for(std::vector<temptest>::iterator it = ptr.begin(); it != ptr.end(); it ++)
{
printf("it delete\n");
}
printf("\n//\n");
ptr.clear();
printf("over\n");
}
运行结果
be build 1
be build 2
be build 3
be build 4
be build 5
//
be build 6
be destory 1 ///////出现了意义不明的析构调用
be destory 2
be destory 3
be destory 4
be destory 5
be destory 6
//
delete
be destory 1 ///////同上也出现了意义不明的析构调用
delete
be destory 2
delete
be destory 3
delete
be destory 4
delete
be destory 5
delete
be destory 6
//
it delete
it delete
it delete
it delete
it delete
it delete
//
be destory 1
be destory 2
be destory 3
be destory 4
be destory 5
be destory 6
over
将拷贝构造函数加入
#include <stdarg.h>
#include <stdio.h>
#include <stdint.h>
#include <iostream>
#include <vector>
int count = 1;
class temptest
{
public:
temptest();
temptest(temptest const& other)
{
test = count;
printf("copy be build %d\n", test);
count ++;
}
~temptest();
int check();
private:
uint8_t check_ = 255;
uint8_t test;
};
temptest::temptest(/* args */)
{
test = count;
printf("be build %d\n", test);
count ++;
}
temptest::~temptest()
{
printf("be destory %d\n", test);
}
int temptest::check()
{
uint8_t test_temp = (~test) + 1;
if(test_temp == check_)
{
printf("okok:%d, %d, %d\n", test_temp, test, check_);
return 1;
}
else
{
printf("nonono:%d, %d, %d\n", test_temp, test, check_);
return 0;
}
}
temptest getInst()
{
return temptest();
}
int main()
{
std::vector<temptest> ptr(5);
printf("\n//\n");
ptr.push_back(getInst());
printf("\n//\n");
for(temptest test : ptr)
{
printf("delete\n");
}
printf("\n//\n");
for(std::vector<temptest>::iterator it = ptr.begin(); it != ptr.end(); it ++)
{
printf("it delete\n");
}
printf("\n//\n");
ptr.clear();
printf("over\n");
}
be build 1
be build 2
be build 3
be build 4
be build 5 ///////调用普通构造函数
//
be build 6 ///////调用构造函数
copy be build 7 ///////vector扩容时调用拷贝构造函数创建的对象 7 8 9 10 11 12
copy be build 8
copy be build 9
copy be build 10
copy be build 11
copy be build 12
be destory 1
be destory 2
be destory 3
be destory 4
be destory 5
be destory 6
///////在vector容量不够的时候,先扩容,再调用拷贝构造函数把之前的内容送到新的数组中
///////再调用析构函数
///////如果没有写拷贝构造函数 temptest(temptest const& other)
///////则会直接调用默认拷贝构造函数,这个是编译器默认添加的
///////如果没写拷贝构造函数,就会看到一堆析构函数被调用,看不到构造函数被调用
//
copy be build 13
delete
be destory 13
copy be build 14
delete
be destory 14
copy be build 15
delete
be destory 15
copy be build 16
delete
be destory 16
copy be build 17
delete
be destory 17
copy be build 18
delete
be destory 18 ///////在foreach中会通过拷贝构造函数创建临时temptest test对象,然后在for循环结束后析构掉
//
it delete
it delete
it delete
it delete
it delete
it delete ///////迭代器iterator不会构建任何对象,只是通过指针的方式调用
//
be destory 8 ///////ptr.clear(); 中全部析构的对象
be destory 9
be destory 10
be destory 11
be destory 12
be destory 7
over