跳转到内容

C++ 修饰符

来自代码酷

C++修饰符[编辑 | 编辑源代码]

C++修饰符是用于改变变量、函数或类成员基本特性的关键字。它们可以调整存储方式、访问权限或行为特征,是C++类型系统的重要组成部分。本条目将详细介绍C++中的各种修饰符及其应用场景。

基本分类[编辑 | 编辑源代码]

C++修饰符主要分为以下几类:

1. 访问修饰符[编辑 | 编辑源代码]

控制类成员的访问权限:

  • public
  • private
  • protected

2. 存储类修饰符[编辑 | 编辑源代码]

控制变量的生命周期和可见性:

  • auto(C++11后含义改变)
  • register(C++17弃用)
  • static
  • extern
  • thread_local(C++11)
  • mutable

3. 类型修饰符[编辑 | 编辑源代码]

改变基本类型的特性:

  • const
  • volatile
  • signed
  • unsigned
  • long
  • short

详细说明[编辑 | 编辑源代码]

访问修饰符[编辑 | 编辑源代码]

控制类成员的访问权限:

class Example {
public:    // 公有成员,任何代码都可访问
    int publicVar;
    
private:   // 私有成员,仅类内和友元可访问
    int privateVar;
    
protected: // 保护成员,类内、派生类和友元可访问
    int protectedVar;
};

存储类修饰符[编辑 | 编辑源代码]

static[编辑 | 编辑源代码]

使变量具有静态存储期,或限制符号的链接性:

// 文件作用域的static变量
static int fileScoped = 42; // 内部链接

void func() {
    static int count = 0; // 保持值不变,仅初始化一次
    ++count;
    std::cout << "Called " << count << " times\n";
}

extern[编辑 | 编辑源代码]

声明变量在其他文件中定义:

// file1.cpp
int globalVar = 10;

// file2.cpp
extern int globalVar; // 使用file1.cpp中定义的globalVar

mutable[编辑 | 编辑源代码]

允许const成员函数修改特定成员:

class Cache {
    mutable int cacheValue; // 即使const函数也可修改
public:
    int getValue() const {
        if (!cacheValid) {
            cacheValue = computeValue(); // 允许修改
            cacheValid = true;
        }
        return cacheValue;
    }
};

类型修饰符[编辑 | 编辑源代码]

= const[编辑 | 编辑源代码]

创建不可修改的对象:

const int MAX_SIZE = 100; // 必须初始化
const int* ptr = &MAX_SIZE; // 指向const的指针
int* const ptr2 = &var; // const指针
const int* const ptr3 = &MAX_SIZE; // const指针指向const数据

= volatile[编辑 | 编辑源代码]

防止编译器优化,用于硬件访问或多线程:

volatile bool interruptFlag = false;
// 编译器不会优化对此变量的访问

= signed/unsigned[编辑 | 编辑源代码]

改变整型的符号特性:

unsigned int positiveOnly = 42;
signed int normalInt = -10;

组合使用[编辑 | 编辑源代码]

修饰符可以组合使用,但需注意顺序和兼容性:

static const int SCREEN_WIDTH = 1024; // 静态常量
extern volatile bool systemRunning;   // 外部易变变量

实际应用案例[编辑 | 编辑源代码]

1. 单例模式中的static[编辑 | 编辑源代码]

class Singleton {
private:
    static Singleton* instance; // 静态成员
    Singleton() {} // 私有构造函数
    
public:
    static Singleton* getInstance() {
        if (!instance) {
            instance = new Singleton();
        }
        return instance;
    }
};

Singleton* Singleton::instance = nullptr; // 静态成员初始化

2. 配置常量中的constexpr[编辑 | 编辑源代码]

class Config {
public:
    static constexpr int MAX_CONNECTIONS = 100; // 编译时常量
    static constexpr double TIMEOUT = 2.5;
};

3. 多线程中的volatile[编辑 | 编辑源代码]

volatile bool shouldStop = false;

void workerThread() {
    while (!shouldStop) {
        // 执行任务
    }
}

// 其他线程可以设置shouldStop=true来停止工作线程

修饰符关系图[编辑 | 编辑源代码]

graph TD A[C++修饰符] --> B[访问修饰符] A --> C[存储类修饰符] A --> D[类型修饰符] B --> E[public] B --> F[private] B --> G[protected] C --> H[static] C --> I[extern] C --> J[mutable] C --> K[thread_local] D --> L[const] D --> M[volatile] D --> N[signed/unsigned] D --> O[long/short]

最佳实践[编辑 | 编辑源代码]

1. 优先使用const来保证不可变性 2. 谨慎使用mutable,仅在真正需要时 3. 避免使用已弃用的register 4. 在多线程环境中正确使用volatile和原子类型 5. 合理使用static减少全局变量污染

常见问题[编辑 | 编辑源代码]

Q: const和constexpr有什么区别? A: const表示运行时常量,constexpr表示编译时常量。

Q: 为什么C++17弃用了register? A: 现代编译器能自动优化寄存器分配,此关键字已无实际作用。

Q: mutable会破坏const正确性吗? A: 不会,它只是为特定场景(如缓存)提供灵活性,但仍应谨慎使用。

总结[编辑 | 编辑源代码]

C++修饰符提供了对程序元素行为的精细控制。理解并正确使用这些修饰符是写出高效、安全C++代码的关键。建议通过实际编码练习来掌握各种修饰符的组合使用方式。