C++ 类与对象:this指针与const成员函数详解
一、类结构设计示例以下是一个复数类Complex的设计展示了私有成员、公有成员函数、重载以及 this 指针的应用class Complex { private: double real; // 实部 double image; // 虚部 public: // 设置成员 void setReal(double r) { this-real r; } void setImage(double i) { this-image i; } // 常成员函数取值 double getReal() const { return this-real; } double getImage() const { return this-image; } // 打印复数 void PrintInfo() const { cout ( real (image 0 ? : ) image i) endl; } // 加法1值传递参数 Complex Add(Complex c) { Complex tmp; tmp.real this-real c.real; tmp.image this-image c.image; return tmp; } // 加法2const引用参数const成员 Complex Add(const Complex c) const { Complex tmp; tmp.real this-real c.real; tmp.image this-image c.image; return tmp; } // 运算符重载 Complex operator(const Complex c) const { Complex tmp; tmp.real this-real c.real; tmp.image this-image c.image; return tmp; } }; int main() { Complex c1, c2, c3; c1.setReal(1.2); c1.setImage(3.4); c2.setReal(3.4); c2.setImage(5.6); c1.PrintInfo(); c2.PrintInfo(); // 重载调用c3 c1.operator(c2) c3 c1 c2; c3.PrintInfo(); // 输出 (4.69i) return 0; }二、类与对象内存模型引出 this 指针内存节省原理所有同类对象共用一份成员函数代码每个对象只独立存储私有数据成员避免每个对象复制函数造成内存浪费。关键疑问共用代码如何区分不同对象的数据答案编译器自动给非静态成员函数添加隐藏形参this指针调用时自动传入当前对象地址。c1.setReal(1.2)→ 编译转换setReal(c1, 1.2)c2.setReal(3.4)→ 编译转换setReal(c2, 3.4)函数内real r等价this-real r三、this 指针详解1. this 基础属性存在范围仅非静态成员函数自带隐式 this全局函数、静态成员函数、友元无 this。指针类型普通成员函数类名* const thisconst 修饰成员函数const 类名* const thisconst this 含义*const this指针本身地址不可修改不能this NULLconst 类名*不能通过 this 修改对象成员常成员函数只读不能手动声明 this由编译器自动生成。2. const 成员函数this 被 const 修饰语法函数括号后加const例double getReal() const;约束const 成员函数内不能修改任何非静态成员变量除非变量被mutable修饰。调用权限const 对象只能调用 const 成员函数非 const 对象可以调用 const 和非 const 成员函数。重载区分const 和非 const 版本可以构成重载编译器根据调用对象的 const 性质选择。3. this 指针的典型应用场景区分同名形参void setReal(double real) { this-real real; }返回当前对象引用用于链式调用Complex setReal(double r) { this-real r; return *this; }在成员函数中传递自身如obj1.compare(obj2)内部可能需要访问this与参数比较。四、综合示例Point 与 Line 类以下代码进一步展示了构造函数、成员函数、this 指针在实际类设计中的运用// Point 点类 class Point { private: float x; // x坐标 float y; // y坐标 public: // 1. 无参构造函数默认 (0, 0) Point() { x 0; y 0; } // 2. 带参构造函数自定义坐标 Point(float xVal, float yVal) { x xVal; y yVal; } // 设置x、y void setX(float xVal) { x xVal; } void setY(float yVal) { y yVal; } // 获取x、y常成员函数 float getX() const { return x; } float getY() const { return y; } // 打印坐标点 void print() const { cout ( x , y ); } }; // Line 线段类 class Line { private: Point a; // 起点 Point b; // 终点 public: // 1. 无参构造两个点都默认为 (0,0) Line() { // a 和 b 自动调用 Point 的无参构造 } // 2. 带参构造直接传入四个坐标值 Line(float x1, float y1, float x2, float y2) : a(x1, y1), b(x2, y2) // 初始化列表给 a、b 赋值 { } // 设置起点、终点 void setA(Point p) { a p; } void setB(Point p) { b p; } // 获取起点、终点 Point getA() const { return a; } Point getB() const { return b; } // 核心计算线段长度 float getLength() const { float dx a.getX() - b.getX(); float dy a.getY() - b.getY(); return sqrt(dx * dx dy * dy); } // 打印线段信息 void printLine() const { cout 线段起点; a.print(); cout 终点; b.print(); cout 长度 getLength() endl; } }; int main() { // 测试 Point Point p1(1, 1); Point p2(4, 5); cout 点信息 endl; p1.print(); cout endl; p2.print(); cout endl; // 测试 Line Line line1(p1.getX(), p1.getY(), p2.getX(), p2.getY()); Line line2(0, 0, 3, 4); cout \n 线段信息 endl; line1.printLine(); line2.printLine(); return 0; }五、总结this 指针是 C 实现对象间数据隔离与代码共享的关键机制理解其隐式传递原理、const 修饰规则以及典型应用场景是掌握面向对象编程的基础。结合 Point、Line、Complex 等类的设计可以清晰看到构造函数、成员函数、运算符重载与 this 指针如何协同工作构建出安全、高效且易用的自定义类型。