C++学习笔记——初始化列表、创建和实例化对象、new 关键字、隐式构造与 explicit 关键字、运算符与运算符重载
目录1. 初始化列表1.1 基本语法1.2 为什么使用初始化列表1.3 初始化顺序2. 创建和实例化对象2.1 栈上分配自动存储期2.2 堆上分配动态存储期2.3 栈 vs 堆Cherno 的建议3. new 关键字3.1 new 的作用3.2 new 与 malloc 的区别3.3 数组形式的 new 与 delete3.4 定位 newplacement new4. 隐式构造与 explicit 关键字4.1 隐式构造隐式类型转换4.2 explicit 关键字5. 运算符与运算符重载5.1 基本概念5.2 成员函数 vs 全局函数5.3 常用运算符重载示例1. 初始化列表1.1 基本语法初始化列表位于构造函数参数列表之后、函数体之前以冒号:开头多个成员用逗号分隔。class Entity { private: std::string m_Name; int m_Age; public: Entity(const std::string name, int age) : m_Name(name), m_Age(age) // 初始化列表 { // 构造函数体 } };1.2 为什么使用初始化列表效率成员变量在进入构造函数体之前已经完成初始化直接调用其构造函数而不是先默认构造再在函数体内赋值。必须使用初始化列表的场景const成员变量引用成员变量没有默认构造函数的成员对象基类构造函数尤其是需要带参数的基类class Base { public: Base(int x) {} }; class Derived : public Base { private: const int m_ConstVal; int m_Ref; public: Derived(int a, int ref) : Base(a), m_ConstVal(a), m_Ref(ref) // 必须使用初始化列表 { } };1.3 初始化顺序初始化顺序按照成员在类中声明的顺序而不是初始化列表的顺序。为避免依赖未初始化的成员应保证顺序一致。class Example { int m_A; int m_B; public: Example() : m_B(2), m_A(m_B) {} // 危险m_A 先初始化此时 m_B 尚未初始化 };2. 创建和实例化对象2.1 栈上分配自动存储期语法Entity e;或Entity e(name);生命周期在作用域结束时自动调用析构函数并释放内存。优点极快仅栈指针移动无需手动管理内存。缺点生命周期局限于作用域对象大小受栈空间限制通常几 MB。void func() { Entity e; // 栈上创建自动调用构造函数 e.DoSomething(); } // 自动调用析构函数2.2 堆上分配动态存储期语法Entity* e new Entity();或new Entity(name)生命周期需显式调用delete e;释放否则内存泄漏。优点对象生命周期可控可跨作用域存在适合大型对象或需要动态创建的场景。缺点速度慢需堆分配、内存管理开销容易造成内存泄漏或悬空指针。Entity* createEntity() { Entity* e new Entity(Cherno); return e; // 对象在堆上函数返回后仍然存在 } void use() { Entity* e createEntity(); e-DoSomething(); delete e; // 必须手动释放 }2.3 栈 vs 堆Cherno 的建议优先使用栈分配除非确实需要动态生命周期如多态、对象数量不确定、跨函数传递所有权。如果需要动态分配现代 C 应使用智能指针std::unique_ptr、std::shared_ptr代替裸指针避免手动delete。3. new 关键字3.1 new 的作用分配内存调用operator new类似malloc分配足够的内存。调用构造函数在该内存上构造对象。返回指向该对象的指针。3.2 new 与 malloc 的区别特性newmalloc内存来源自由存储区通常是堆堆是否调用构造函数✅❌返回类型类型安全的指针T*void*内存大小自动计算sizeof(T)需手动指定字节数失败处理抛出std::bad_alloc异常返回nullptr3.3 数组形式的 new 与 deleteint* arr new int[10]; // 分配 10 个 int 的数组 delete[] arr; // 必须使用 delete[]保证调用每个元素的析构函数3.4 定位 newplacement new可以在已分配的内存上构造对象常用于自定义内存管理。char* buffer new char[sizeof(Entity)]; Entity* e new (buffer) Entity(Cherno); // 在 buffer 位置构造对象 e-~Entity(); // 手动调用析构 delete[] buffer;4. 隐式构造与 explicit 关键字4.1 隐式构造隐式类型转换隐式构造当构造函数只接受一个参数或除第一个参数外都有默认值编译器允许在需要该类型对象的地方使用该参数类型自动调用构造函数进行隐式转换。隐式转换可能造成意料之外的对象构造降低代码可读性甚至引发隐蔽的错误。4.2 explicit 关键字作用在构造函数前加explicit禁止该构造函数参与隐式类型转换。class Object { private: std::string m_Name; int m_Age; public: explicit Object(const const std::string name) : m_Name(name), m_Age(-1) {} Object(int age) : m_Name(Unknown), m_Age(age) {} }; void PrintObj(const Object obj) { } int main() { //隐式构造 PrintObj(1); Object obj 22; //explicit 会禁止隐式构造 //PrintObj(std::string(Cheer));会报错 //Object obj std::string(Cheer); std::cin.get(); }5. 运算符与运算符重载5.1 基本概念运算符重载允许为用户定义的类型提供自定义的运算符行为。可以重载的运算符包括算术运算符、关系运算符、逻辑运算符、位运算符、赋值运算符、下标运算符、函数调用运算符等。某些运算符不能重载如::、.、.*、?:、sizeof等。5.2 成员函数 vs 全局函数成员函数左操作数是当前对象this。全局函数可灵活支持隐式类型转换通常需要声明为友元。class Vector2 { public: float x, y; Vector2 operator(const Vector2 other) const { // 成员函数 return {x other.x, y other.y}; } }; // 全局函数版本通常用于对称性如实现 operator Vector2 operator-(const Vector2 a, const Vector2 b) { return {a.x - b.x, a.y - b.y}; }5.3 常用运算符重载示例赋值运算符class String { char* m_Data; public: String operator(const String other) { if (this ! other) { // 自赋值检查 delete[] m_Data; m_Data new char[strlen(other.m_Data) 1]; strcpy(m_Data, other.m_Data); } return *this; } };流插入与流提取class Point { int m_X, m_Y; public: friend std::ostream operator(std::ostream os, const Point p); }; std::ostream operator(std::ostream os, const Point p) { os ( p.m_X , p.m_Y ); return os; }下标运算符[]class Array { int data[100]; public: int operator[](int index) { return data[index]; } const int operator[](int index) const { return data[index]; } };函数调用运算符()class Multiply { public: int operator()(int a, int b) const { return a * b; } }; Multiply mul; int result mul(3, 4); // 12
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2474659.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!