#include <iostream>
#include <vector>
using namespace std;
class student {
public:
student(int age,const char *name) {
this->age = age;
strncpy_s(this->name, name, 64);
cout << "年龄:" << this->age << "姓名:" << this->name << endl;
cout << "有参构造函数被调用..." << endl;
}
student(const student& other) {
this->age = other.age;
strncpy_s(this->name, other.name, 64);
cout << "拷贝构造函数被调用..." << endl;
}
~student() {
cout << "析构函数被调用..." << endl;
}
private:
int age;
char name[64];
};
int main(void) {
vector<student> stu1;
//先定义再插入
//student x(18, "李小花");
//stu1.push_back(x);
//直接插入临时对象
//stu1.push_back(student(20, "王二狗"));
//c++11新特性 变参模板和完美转发 只调用有参构造函数
stu1.emplace_back(19, "王大翠");
stu1.emplace(stu1.end(), 18, "王二五");
system("pause");
return 0;
}
变参模板和完美转发
最新推荐文章于 2024-08-14 17:18:35 发布