#include <iostream>
#include <vector>
#include <algorithm>
class my {
public:
my() {
counter++;
std::cout << "class constructor" << counter << " \n";}
~my() {
std::cout << "class destructor" << counter << " \n";
counter--;
}
static inline int counter = 0;
};
int main()
{
my v1;
std::vector<my> my_vec;
my * p = new my();
my_vec.push_back(std::move(*p));
my_vec.push_back(std::move(v1));
}
简单的例子,但是我不明白我做错了什么,结果我得到了比我预期的2个额外的析构函数(预期2)。有人可以解释一下吗?
结果:
class constructor1
class constructor2
class destructor2
class destructor1
class destructor0
class destructor-1