#include<iostream>
#include<vector>
class test {
public:
virtual ~test()
{
std::cout<<"inside destructor \n";
}
};
int main()
{
std::cout<<"---with universal initializer--------- \n";
{
test t1;
std::vector<test> v{t1};
}
std::cout<<"----- ending universal initailizer ----- \n";
std::cout<<"------------------------\n ";
test t2;
std::vector<test> v2(1,t2);
return 0;
}
输出:
---with universal initializer---------
inside destructor
inside destructor
inside destructor
----- ending universal initailizer -----
------------------------
inside destructor
inside destructor
我的问题是为什么在使用通用初始化完成初始化时,与其他初始化相比,析构函数被称为一个额外的时间或创建了额外的对象