写C++最怕啥?不是写不出功能,而是代码能跑但被review时,同事一句"你这为啥不用XX"直接问懵。下面这5个"进阶偏基础"的知识点,冷门但实用,配上极简代码,让你悄悄提升代码质感~
1. 前向声明:头文件依赖的"减负神器"
还在头文件里疯狂 #include ?前向声明能直接减少编译依赖,解决循环依赖还能提速!
cpp
// forward.h(集中存放前向声明)
#pragma once
class Document; // 无需包含完整定义,声明即可
// my_class.h
#include "forward.h"
#include <memory>
class MyClass {
std::unique_ptr<Document> doc; // 指针/引用类型只需前向声明
};
核心优势:改一个头文件不用全工程重编,编译速度直接翻倍。
2. explicit:阻止隐式转换的"安全锁"
单参数构造函数容易被隐式转换坑?加个explicit直接禁用,避免语义误解。
cpp
// 坑人写法:int会被隐式转为String(10被当作长度)
class String {
public:
String(int n) { /* 分配n个字符空间 */ }
};
print(String(10)); // 正常
print(10); // 意外触发隐式转换,语义混乱
// 安全写法:加explicit禁止隐式转换
class String {
public:
explicit String(int n) { /* 分配n个字符空间 */ }
};
// print(10); // 编译报错,杜绝意外
3. 静态工厂方法:对象创建的"失败处理大师"
构造函数没法返回错误?静态工厂方法能优雅处理创建失败,避免部分初始化对象。
cpp
#include <optional>
#include <fstream>
class FileProcessor {
public:
// 静态工厂方法,创建失败返回nullopt
static std::optional<FileProcessor> create(const std::string& filename) {
std::ifstream file(filename);
if (!file.is_open()) return std::nullopt;
return FileProcessor(std::move(file)); // 私有构造确保初始化成功
}
private:
explicit FileProcessor(std::ifstream file) : file_(std::move(file)) {}
std::ifstream file_;
};
// 使用:创建成功才执行逻辑
if (auto processor = FileProcessor::create("data.txt")) {
processor->process();
}
4. 作用域守卫:资源清理的"自动管家"
怕函数中途return导致资源泄漏?作用域守卫确保离开作用域时自动清理。
cpp
#include <functional>
class ScopeGuard {
public:
explicit ScopeGuard(std::function<void()> cleanup)
: cleanup_(std::move(cleanup)), active_(true) {}
~ScopeGuard() { if (active_) cleanup_(); }
private:
std::function<void()> cleanup_;
bool active_;
};
void writeFile() {
FILE* file = fopen("temp.txt", "w");
ScopeGuard guard([&]() { if (file) fclose(file); }); // 自动关文件
if (someError()) return; // 即使提前返回,文件也会关闭
}
5. std::weak_ptr:打破智能指针的"循环引用"
shared_ptr 互相引用会导致内存泄漏? weak_ptr 不增加引用计数,轻松破局。
cpp
#include <memory>
struct A { std::shared_ptr<B> b; };
struct B {
std::weak_ptr<A> a; // 改为weak_ptr,不增加引用计数
};
int main() {
auto a = std::make_shared<A>();
auto b = std::make_shared<B>();
a->b = b;
b->a = a; // 不再造成循环引用,离开作用域正常释放
}