[c++] IKM
2023-09-27 14:23:24 时间
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
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#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。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 结构体
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
int main(void) { struct { int yy, mm, dd; -- } data; data.yy = 1; return 0; }
常函数
可以使用数据成员,不能进行修改。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#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.
相关文章
- 复盘:C++和java中,面向对象和面向过程的区别
- C++下批量读取名字无规则的图片文件的示例代码(利用类WIN32_FIND_DATA实现)
- C#调用C++(opencv)中图片数据传递的问题
- C/C++中如何获取数组的长度?
- c++11 Using Callable Objects, std::thread, std::bind, std::async, std::call_once
- 《像计算机科学家一样思考C++》——1.4 形式语言和自然语言
- 编译c/c++完整工具链
- C++编译器会对没有构造函数的类生成默认构造函数吗?(有必要的时候才生成,要看情况。有反汇编验证)
- 144、【动态规划】leetcode ——509. 斐波那契数:递归法+迭代法(C++版本)