ChatGPT 开发者如何快速接入 Taotoken 并调用多模型服务
2026/5/8 18:30:29
创建对象时自动调用,负责初始化成员变量。
cpp
运行
class Person { public: // 普通构造函数 Person() { cout << "默认构造函数" << endl; } // 有参构造 Person(int a) { cout << "有参构造函数" << endl; } }; int main() { Person p1; // 调用默认构造 Person p2(18); // 调用有参构造 return 0; }对象销毁时自动调用,负责释放资源(堆内存、文件、网络等)。
~,和类名一样cpp
运行
class Person { public: Person() { cout << "构造函数" << endl; } ~Person() { cout << "析构函数,对象销毁" << endl; } }; int main() { Person p; // 函数结束 p 销毁,自动调用析构 return 0; }用已有对象初始化新对象,复制一份一模一样的对象。
cpp
运行
类名(const 类名& 旧对象)cpp
运行
class Person { public: int age; // 拷贝构造函数 Person(const Person& other) { age = other.age; cout << "拷贝构造函数" << endl; } Person(int a) : age(a) {} }; int main() { Person p1(20); Person p2 = p1; // 触发拷贝构造 return 0; }把临时对象 / 即将销毁对象的资源直接转移给新对象,不复制,只 “搬家”,效率极高。
cpp
运行
类名(类名&& 临时对象)cpp
运行
class Person { public: // 移动构造 Person(Person&& other) { cout << "移动构造函数" << endl; } }; // 返回临时对象 Person createObj() { return Person(); } int main() { Person p = createObj(); // 触发移动构造 return 0; }