C++类与对象进阶核心技巧

好的,以下是一份关于C++类与对象的进阶笔记:


一、构造函数进阶

1. 初始化列表
  • 用于高效初始化成员变量,特别是const成员和引用成员。
  • 语法:ClassName::ClassName(args) : member1(value1), member2(value2) {...}
cpp 复制代码
class Point {
public:
    Point(int x, int y) : x_(x), y_(y) {} // 直接初始化
private:
    int x_;
    int y_;
};
2. 委托构造函数
  • 一个构造函数调用同一类的其他构造函数。
cpp 复制代码
class Box {
public:
    Box() : Box(0, 0, 0) {} // 委托给三参数构造函数
    Box(int l, int w, int h) : length(l), width(w), height(h) {}
private:
    int length, width, height;
};

二、static成员

1. 静态数据成员
  • 属于类而非对象,所有对象共享同一份数据。
  • 需在类外初始化:
cpp 复制代码
class Counter {
public:
    static int count; // 声明
};
int Counter::count = 0; // 类外初始化
2. 静态成员函数
  • this指针,仅能访问静态成员。
cpp 复制代码
class Math {
public:
    static double square(double x) { return x * x; }
};

三、友元(friend

1. 友元函数
  • 非成员函数可访问类的私有成员。
cpp 复制代码
class Rectangle {
private:
    int width, height;
    friend void printArea(const Rectangle& r); // 声明友元
};
void printArea(const Rectangle& r) {
    std::cout << r.width * r.height; // 直接访问私有成员
}
2. 友元类
  • 整个类可访问当前类的私有成员。
cpp 复制代码
class A {
    friend class B; // B是A的友元类
private:
    int secret;
};
class B {
public:
    void showSecret(A& a) { std::cout << a.secret; }
};

四、运算符重载

1. 基本语法
  • 通过成员函数或全局函数重载运算符。
cpp 复制代码
class Vector {
public:
    Vector operator+(const Vector& other) { // 成员函数重载+
        return Vector(x + other.x, y + other.y);
    }
private:
    int x, y;
};
2. 输入/输出运算符重载
cpp 复制代码
friend std::ostream& operator<<(std::ostream& os, const Vector& v) {
    os << "(" << v.x << ", " << v.y << ")";
    return os;
}

五、继承与多态

1. 虚函数与多态
  • 虚函数实现运行时多态。
cpp 复制代码
class Shape {
public:
    virtual double area() const = 0; // 纯虚函数
};
class Circle : public Shape {
public:
    double area() const override { return 3.14 * radius * radius; }
};
2. 虚函数表(vtable)
  • 编译器为含虚函数的类生成虚函数表,存储函数指针。
  • 对象通过虚指针(vptr)访问vtable

六、深拷贝与浅拷贝

1. 拷贝构造函数
  • 默认浅拷贝可能引发资源重复释放。
  • 需自定义深拷贝:
cpp 复制代码
class String {
public:
    String(const String& other) {
        data = new char[strlen(other.data) + 1];
        strcpy(data, other.data);
    }
private:
    char* data;
};

七、const成员函数

  • 保证函数不修改对象状态。
cpp 复制代码
class Student {
public:
    int getScore() const { return score; } // 不会修改成员
private:
    int score;
};

八、移动语义(C++11)

1. 右值引用
  • 语法:T&&,用于绑定临时对象。
cpp 复制代码
void func(std::string&& s) { // s是右值引用
    std::string data = std::move(s); // 转移资源
}
2. 移动构造函数
  • 高效转移资源,避免深拷贝。
cpp 复制代码
class Buffer {
public:
    Buffer(Buffer&& other) : ptr(other.ptr) { 
        other.ptr = nullptr; // 置空原指针
    }
private:
    int* ptr;
};

这份笔记涵盖了类与对象的核心进阶知识,建议结合代码实践加深理解。

相关推荐
LuminousCPP20 分钟前
单链表专题(四)-刷题复盘篇-LeetCode 138 随机链表复制|原地拷贝法突破复杂指针操作
数据结构·笔记·算法·leetcode·链表
逆境不可逃42 分钟前
【LeetCode 912】排序数组——随机化快速排序详解
数据结构·算法·排序算法
Coder-magician1 小时前
《代码随想录》刷题打卡day31:动态规划-背包问题part02
算法·动态规划
薛定e的猫咪1 小时前
从因果视角解决多智能体协作:细读 SCIC 算法
算法
c238561 小时前
《算法武林谱:四大排序神功与二分寻宝术全解》
数据结构·算法·排序算法
一米阳光86611 小时前
软考(中级)软件设计师核心笔记(9)算法——时间复杂度与空间复杂度、查找算法、排序算法
笔记·算法·职场发展·软考·软件设计师·中级职称
Mem0rin1 小时前
二分查找:左右边界
数据结构·算法
大明者省11 小时前
WSL2 Ubuntu22.04 GPU训练环境配置指南
人工智能·算法·计算机视觉
白狐_79813 小时前
408数据结构第8章:排序②——性质对比秒杀、场景选择与外部排序
java·数据结构·算法
zander25813 小时前
LeetCode 84:柱状图中的最大矩形——单调栈如何确定左右边界
java·数据结构·算法