#include <iostream>
#include <vector>
class Car{
public:
int weight;
Car(int weight): weight(weight){
};
Car(const Car& other){
std::cout<<"copied!"<<std::endl;
this->weight=other.weight;
}
};
int main() {
std::vector<Car> vec;
vec.push_back(Car(1));
return 0;
}
在上面的代码中,当我将 Car(1) 推送到 vec 上时,不知何故 Car 类中的复制构造函数被调用。为什么要调用复制构造函数?我这里没有复制任何东西。