掌握这些核心知识点,轻松应对90%的C++技术面试
一、基础语法与关键字
1.1 const关键字的多种用法
// 1. 常量变量
const int MAX_SIZE = 100;
// 2. 常量指针与指针常量
const int* ptr1 = &var; // 指向常量的指针
int* const ptr2 = &var; // 常量指针
const int* const ptr3 = &var; // 指向常量的常量指针
// 3. 常量成员函数
class MyClass {
public:
int getValue() const; // 保证不修改成员变量
};
1.2 static关键字的四种应用场景
// 1. 静态局部变量
void func() {
static int count = 0; // 只初始化一次
count++;
}
// 2. 静态成员变量
class MyClass {
public:
static int sharedValue; // 所有实例共享
};
// 3. 静态成员函数
class MyClass {
public:
static void utilityFunc(); // 不依赖于特定实例
};
// 4. 静态全局变量
static int fileScopeVar; // 文件作用域
二、面向对象编程核心概念
2.1 构造函数与析构函数
class Person {
public:
// 构造函数
Person() : age(0) {} // 默认构造
Person(int a) : age(a) {} // 参数化构造
Person(const Person& other) : age(other.age) {} // 拷贝构造
// 析构函数
~Person() {
// 资源清理代码
}
private:
int age;
};
2.2 虚函数与多态
class Shape {
public:
virtual void draw() const = 0; // 纯虚函数
virtual ~Shape() {} // 虚析构函数
};
class Circle : public Shape {
public:
void draw() const override { // 重写虚函数
cout << "Drawing Circle" << endl;
}
};
// 使用多态
Shape* shape = new Circle();
shape->draw(); // 输出 "Drawing Circle"
delete shape;
三、内存管理深度解析
3.1 堆与栈内存对比
|
特性 |
栈内存 |
堆内存 |
|
分配方式 |
自动分配/释放 |
手动分配/释放 |
|
大小限制 |
较小(通常几MB) |
较大(受系统内存限制) |
|
访问速度 |
快 |
慢 |
|
碎片问题 |
无 |
可能产生碎片 |
|
作用域 |
局部作用域 |
全局作用域 |
3.2 智能指针(C++11起)
#include <memory>
// 1. unique_ptr: 独占所有权
std::unique_ptr<int> uptr(new int(10));
// 2. shared_ptr: 共享所有权
std::shared_ptr<int> sptr1 = std::make_shared<int>(20);
std::shared_ptr<int> sptr2 = sptr1; // 共享所有权
// 3. weak_ptr: 解决循环引用
std::weak_ptr<int> wptr = sptr1;
if(auto temp = wptr.lock()) {
// 使用temp访问资源
}
四、模板与泛型编程
4.1 函数模板
template <typename T>
T max(T a, T b) {
return (a > b) ? a : b;
}
// 使用
int i = max(10, 5); // T 推导为 int
double d = max(3.14, 2.72); // T 推导为 double
4.2 类模板与特化
template <typename T>
class Box {
public:
void setContent(T t) { content = t; }
T getContent() { return content; }
private:
T content;
};
// 特化版本
template <>
class Box<char> {
public:
void setContent(char c) { content = c; }
char getContent() { return content; }
private:
char content;
};
五、STL核心组件
5.1 常用容器对比
|
容器 |
特点 |
适用场景 |
|
vector |
动态数组,随机访问快 |
需要频繁随机访问 |
|
list |
双向链表,插入删除快 |
需要频繁插入删除 |
|
map |
红黑树实现,键值对有序 |
需要有序键值对存储 |
|
unordered_map |
哈希表实现,查找O(1) |
需要快速查找 |
|
deque |
双端队列,两端操作高效 |
需要双端操作 |
5.2 常用算法示例
#include <algorithm>
#include <vector>
vector<int> vec = {5, 3, 1, 4, 2};
// 排序
sort(vec.begin(), vec.end()); // {1, 2, 3, 4, 5}
// 查找
auto it = find(vec.begin(), vec.end(), 3);
if(it != vec.end()) {
// 找到元素
}
// 遍历操作
for_each(vec.begin(), vec.end(), [ ](int n) {
cout << n << " ";
});
六、现代C特性(C11/14/17)
6.1 移动语义与右值引用
class ResourceHolder {
public:
// 移动构造函数
ResourceHolder(ResourceHolder&& other) noexcept
: resource(other.resource) {
other.resource = nullptr;
}
// 移动赋值运算符
ResourceHolder& operator=(ResourceHolder&& other) noexcept {
if(this != &other) {
delete resource;
resource = other.resource;
other.resource = nullptr;
}
return *this;
}
private:
Resource* resource;
};
6.2 Lambda表达式
// 基本语法
auto func = [capture](parameters) -> return_type {
// 函数体
};
// 示例
vector<int> numbers = {1, 2, 3, 4, 5};
int threshold = 3;
// 使用lambda过滤大于threshold的数
auto it = remove_if(numbers.begin(), numbers.end(),
[threshold](int n) {
return n > threshold;
}
);
// 带捕获的lambda
int a = 10;
auto add = [a](int b) { return a + b; };
cout << add(5); // 输出15
七、常见面试题精析
7.1 虚函数表实现原理
C++通过虚函数表(vtable)实现多态:
-
每个包含虚函数的类都有一个虚函数表
-
对象中包含指向虚函数表的指针(vptr)
-
调用虚函数时通过vptr找到对应的函数地址
class Base {
public:
virtual void func1() {}
virtual void func2() {}
};
class Derived : public Base {
public:
void func1() override {}
void func3() {}
};
// 虚函数表示例:
// Base vtable: [&Base::func1, &Base::func2]
// Derived vtable: [&Derived::func1, &Base::func2]
7.2 智能指针循环引用问题
class B; // 前向声明
class A {
public:
shared_ptr<B> b_ptr;
};
class B {
public:
shared_ptr<A> a_ptr;
};
void createCycle() {
auto a = make_shared<A>();
auto b = make_shared<B>();
a->b_ptr = b;
b->a_ptr = a; // 循环引用,内存泄漏!
}
// 解决方案:使用weak_ptr打破循环
class B {
public:
weak_ptr<A> a_ptr; // 改为weak_ptr
};
八、C++20重要新特性
8.1 Concepts(概念)
// 定义概念
template<typename T>
concept Integral = std::is_integral_v<T>;
// 使用概念约束模板
template<Integral T>
T add(T a, T b) {
return a + b;
}
// 编译时检查
add(5, 3); // OK
add(2.5, 1.2); // 编译错误
8.2 Ranges(范围)
#include <ranges>
#include <vector>
#include <algorithm>
vector<int> nums = {1, 2, 3, 4, 5};
// 使用管道操作符组合操作
auto result = nums | views::filter([ ](int n){ return n % 2 == 0; })
| views::transform([ ](int n){ return n * 2; });
// 输出: 4 8
for(int n : result) {
cout << n << " ";
}
总结与学习建议
-
基础为王:牢固掌握指针、内存管理、面向对象基础
-
理解原理:深入理解虚函数、模板、STL实现机制
-
现代特性:熟练掌握C++11/14/17核心特性
-
实践至上:通过实际项目加深理解
-
阅读源码:学习优秀开源项目(如STL实现)的代码
C++学习是一个持续的过程,每天进步一点点,坚持带来大改变!
推荐学习资源:
-
《Effective C++》系列
-
《C++ Primer》
-
CppReference.com
-
Leetcode C++练习
掌握这些核心知识点,相信你能在C++面试中游刃有余!


4万+

被折叠的 条评论
为什么被折叠?



