[c++] IKM
IKM考点
More: 学习笔记之IKM C++ 11
goto
不能在不同函数中使用goto。
NULL的本质
#include<stddef.h>
#include<stdio.h>
/* Define NULL pointer value */ #ifndef NULL #ifdef __cplusplus #define NULL 0 #else /* __cplusplus */ #define NULL ((void *)0) #endif /* __cplusplus */ #endif /* NULL */
sizeof
通常策略,求数组元素的个数。
int iList[] = {1,2,3,4,5,6}; printf("%d\n", sizeof(iList)/sizeof(*iList));
sizeof(int*)占用了8字节?
一般来说,在64位系统下指针长度为8,32位系统下指针长度位4。
类拷贝
类的copy and deep copy。
Goto: [c++] Copy Control

#include<iostream> using namespace std; class Person { public: string *namePtr; int a = 1; int b = 2; Person() {namePtr = new string;} void setName(char *s) {*namePtr = s;} }; int main(void) { Person president; Person chairman; -- president.setName("Tom"); cout << *president.namePtr << endl; chairman = president; -- cout << *chairman.namePtr << endl; cout << chairman.a << endl; cout << chairman.b << endl; return 0; }
调用父类 已被覆盖的方法
B b;
b.A::foo()
namespace
C语言就没有命名空间这个概念,为了防止重名就只能使用各种naming convention,比如各种前缀。
你可以把C++的name space看作是一种语言层面支持的前缀,但是又有层级关系。
比如C中为了防止重名,你可以命名com_mycompany_mydepartment_sort
而C++中对com::mycompany::mydepartment::sort的使用,如果在同一个命名空间下,可以直接使用sort。
Goto: [c++] namespace你可以把C++的name space看作是一种语言层面支持的前缀,但是又有层级关系。
比如C中为了防止重名,你可以命名com_mycompany_mydepartment_sort
而C++中对com::mycompany::mydepartment::sort的使用,如果在同一个命名空间下,可以直接使用sort。
宏
# 转换为字符串
## 是粘贴的意思
#define MyMACRO(jkl, xyz) {#xyz ## #jkl}
类中类
类中类是个新的定义,既然其他成员要用,就把声明放在最前面。也必须是public,不能是protected。

class Bus { public: // class Driver // { // public: // string name; // }; >> static Driver* createDriver() { >> return new Driver; } private: -- int seats; protected: class Driver { public: string name; }; }; int main(void) { >> Bus::Driver *driver = Bus::createDriver(); driver->name = "haojie"; return 0; }
unamed 结构体

int main(void) { struct { int yy, mm, dd; -- } data; data.yy = 1; return 0; }
常函数
可以使用数据成员,不能进行修改。

#include<stdio.h> #include<string.h> #include<iostream> using namespace std; class Cart { -- bool _valid; -- int _fruit; static int _apples; static int _oranges; public: -- Cart(): _fruit(0), _valid(false) { } void addApples(int a) {_apples += a; _valid = false;} void addOranges(int a) {_apples += a; _valid = false;} int getItemCount() const { if (!_valid) { >> _fruit = _apples + _oranges; >> _valid = true; } return _fruit; } }; int Cart::_apples = 0; int Cart::_oranges = 0; int main(void) { Cart const checkOutPerson; Cart shopper; shopper.addApples(2); shopper.addOranges(7); -- int total = checkOutPerson.getItemCount(); return 0; }
一元运算符
减号是二元,负号是一元。
- unary minus(-)
- increment(++)
- decrement(- -)
- NOT(!)
- Addressof operator(&)
- sizeof()
虚析构函数
保证了子类的析构函数被调用。
#include<iostream> using namespace std; class Base{ public: Base(){cout<<"创建Base基类。"<<endl;} virtual ~Base() {cout<<"删除Base基类。"<<endl;} /*虚析构函数*/ }; class Child : public Base{ public: Child() {cout<<"创建Child派生类。"<<endl;} ~Child() {cout<<"删除Child派生类。"<<endl;} }; int main() { cout<<"*********虚析构函数调用示例***********"<<endl; Base *C1 = new Child; delete C1; }
这里使用了虚的,如果是普通的,下面的 "删除Child派生类" 将消失。
$ ./a.out *********虚析构函数调用示例*********** 创建Base基类。 创建Child派生类。 删除Child派生类。 删除Base基类。
结构体初始化
Q72. Which of the following changes must be made to the C++ code below so that the widget class definition is const-correct for its usage in main() ?
-
- A. Change Widget(Widget& w); to Widget(const Widget& w);
- B. Change Gizmo (int p, int r) ; to Gizmo (const int p, const int r);
- C. Change Widget& operator=(Widget& rhs); to Widget& operator=(const Widget& rhs);
- D. Change Widget (Gizmo& g) ; to Widget (const Gizmo& g);
- E. Use const_cast during assignment otherWidget = basicWidget;
#include <iostream> struct Gizmo { int pressure, rpm; Gizmo(int p, int r) : pressure(p), rpm(r) {} // Gizmo(const int p, const int r) : pressure(p), rpm(r) {} }; struct Widget { int temp, speed; Widget() : temp (68), speed(100) {} // Widget(Widget& w); Widget(const Widget& w); Widget& operator=(Widget& rhs); // Widget& operator=(const Widget& rhs); // Widget(Gizmo& g); Widget(const Gizmo& g); }; int main(int argc, char **argv) { const Widget prototype; Widget basicWidget = prototype; // No matching constructor for initialization of 'Widget' Widget otherWidget; otherWidget = basicWidget; const Gizmo gadget(10, 100); Widget thirdWidget(gadget); // No matching constructor for initialization of 'Widget' return 1; }
End.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律