C++

C++ 面向对象笔记

Posted by WHZ0325 on 2023-04-08, Viewed times

照学校课件打的,大概率没什么意思,有空再修改。

C++ 程序结构

多文件编译

将源代码文件放一块儿就行。

1
g++ a.cpp b.cpp c.cpp -o program
包含警戒

保证头文件不被重复定义。

解决方案一:#pragma once

解决方案二:包含警戒。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// a.h
#ifndef A
#define A
...
#endif
// b.h
#ifndef B
#define B
...
#endif
// a.c
#include "a.h"
#include "b.h"
...
// b.c
#include "b.h"
#include "a.h"
...
extern
  • 声明其它文件中的变量或函数。
  • extern "C":要求编译器按照 C 语言的方式编译函数(C++ 为实现函数重载会调整函数名,链接时可能出现问题)。

类型和变量

内置类型

wchar_t:wide char type

推导类型
  • auto:定义时自动推导类型。
  • decltype(expression):给出表达式的返回类型。
typedef 函数

用法:typedef return_type (*new_name)(type...)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <cstdio>
typedef int (*func)(int, int);
int add(int x, int y) {
return x + y;
}
int sub(int x, int y) {
return x - y;
}
int main() {
func f1 = add;
printf("%d\n", f1(1, 2));
f1 = sub;
printf("%d\n", f1(1, 2));
return 0;
}
enum 枚举类型

默认从零开始,名称作用域为全局,不能前置声明。

1
2
3
4
5
6
enum Week { Mon = 1, Tue, Wed, Thur, Fri, Sat, Sun };
int main() {
Week first = Mon;
printf("%d\n", first);
return 0;
}
enum class 类型
  • 作用域不再是全局,需要 :: 才能访问。

  • 不能进行隐式类型转换。

  • 可以前置声明,如果类型不是默认的 int 需要手动修改 enum class Name: std::uint32_t;

    .

enum class 不能与 int 相互转化。注意命名空间。

1
2
3
4
5
6
7
8
9
10
#include <cstdio>
enum class Number: char {
one = '1',
two = '2'
};
int main() {
Number num = Number::one;
putchar((char)num);
return 0;
}
union 共用体
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <cstdio>
union u0 {
unsigned int all: 8;
struct {
unsigned int low: 4;
unsigned int high: 4;
};
};
int main() {
u0 a; a.all = 0xffff;// 16位,其实越界了
printf("%d %d %d\n", a.low, a.high, a.all);// 15 15 255
return 0;
}

(这样取一些位会很方便,然而 sizeof 是不变的……)

引用类型

左值引用:貌似不能修改引用对象的指向。

右值引用:不算清晰,大概就是可以对临时对象进行操作?

初始化(C++11)
1
2
3
struct Type { int a, b; };
int a{10}; Type x{1, 2};
int arr[5]{1, 2, 3, 4, 5};
static 关键字
  • 函数内表示静态变量。
  • 函数外表示仅本文件有效。(不同文件重名会被认为是不同变量,由编译器处理)

指针、数组、引用、常量

指针
  • NULL:C++ 中就是整数 0(C 中是 (void *)0)。
  • nullptr:C++11 中用来避免函数调用时产生二义性。

见:https://blog.csdn.net/u010983763/article/details/53667468。

deference(解引用/逆向引用):起这么厉害的名字,其实就是访问指针中的元素 *p

  • 指针数组:int *arr[5];,一个数组,存放指针。
  • 数组指针:int (*p)[5] = &array;,一个指针,指向一个数组,通过 (*p)[i] 取值。
  • #xx 表示为字符串。
  • ##xx 与前面拼接。
  • #@xx 表示为字符。
  • __LINE__ 表示当前行号。
  • __FILE__ 表示当前文件的绝对路径。
  • __FUNCTION__ 表示当前所在的函数名。(调试时应该会很有用)
常量折叠

const 定义的常量不会被重复定义(编译时被展开)。

指针常量
  • const int *p = &arr;:指向的内容不能修改。
  • int* const p = &arr;:存放的指针不能修改。
  • const int* const p = &arr;:俩都不能改。

未解之谜:

1
2
const int *p = &arr;
int *pt = p;// 可能是这样赋值后可以通过 *pt 修改内容所以就禁止了
const 引用
1
const int &a = 1;//正确

函数参数类型是 const int &x 表明可以传入变量或常量(常数也可),在函数体内不能修改而已。

字符串相关

这两者是不同的:

1
2
char *s = "string";// 是指针,因此可以修改指向
char s[] = "string";// 字符数组,可以修改内容,不能修改指向

md,C++ 好难……


函数

调用方式
  • __cdecl:C 模式,参数从右到左压栈,主调用的函数控制,参数可变,可不指定参数名。
  • __stdcall:参数从右到左压栈,被调用的函数控制,参数不可变。

关于不指定参数名:

1
2
3
void __cdecl print(int) {
puts("hello");
}

只有函数体中用不到该形式参数时可以省略,这种情况下 C 和 C++ 有一些不同:

  • C 语言在声明中可以省略,定义中不可以省略。
  • C++ 在声明中可以省略,定义中也可以省略。
函数重载

缺省函数(调用时使用默认参数):

1
2
3
4
void func(int a = 100) {
printf("%d\n", a);
}
func();// 100
  • 缺省函数不能用来区分同名函数。(缺省函数还可能存在其它歧义行为,均被禁止)
  • 返回值类型不能用来区分同名函数。
  • 引用/指针类型(值类型不行)的 const 可用于区分同名函数:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <cstdio>
void func(int *p) { puts("void func(int *p);"); }
void func(const int *p) { puts("void func(const int *p);"); }
int main() {
int cnt = 0; int *p = &cnt; func(p); return 0;
}
// 输出:void func(int *p);
#include <cstdio>
void func(int *p) { puts("void func(int *p);"); }
void func(const int *p) { puts("void func(const int *p);"); }
int main() {
const int cnt = 0; func(&cnt); return 0;
}
// 输出:void func(const int *p);

关于缺省函数:第一个带缺省值的参数后的每一个参数都要带缺省值(不然会有歧义)。

【上面第三条】关于 const 形参(参数必须是引用/指针类型):

  • 实际参数无 const,形式参数有无 const 都可调用,优先调用无 const 的函数。
  • 实际参数有 const,只能调用形式参数有 const 的函数。
1
2
3
4
5
6
#include <cstdio>
void func(int &a) { puts("B"); }// 优先调用
void func(const int &a) { puts("A"); }// 注释掉上一行才会调用它
int main() {
int a = 1; func(a); return 0;
}
汇编逻辑

函数调用时数据存放在栈中,自顶向下从高位到低位,EBP 寄存器用于指向该函数的栈底,ESP 指针则指向栈顶,上一层函数 EBP 寄存器的值往往存放在当前函数的栈底。

返回值
  • 值类型:都是 const,等价于 const int f();
  • 指针类型:指针 const,指针内容可变,等价于 int* const f();(不是 const int* f();)。
  • 引用返回:int &f();,如果要返回的是一个很大的类那就十分有用了。
1
2
3
4
5
6
#include <cstdio>
int &func() { static int a = 1024; printf("%d\n", a); return a; }
int main() {
int &p = func(); ++p; func(); return 0;
}
// 输出:1024\n1025\n

写成这样也是可以的:

1
2
3
4
5
6
#include <cstdio>
int &func() { static int a = 1024; printf("%d\n", a); return a; }
int main() {
++func(); func(); return 0;
}
// 输出:1024\n1025\n

而改为:

1
const int &func() { static int a = 1024; printf("%d\n", a); return a; }

就会报错。

前置声明
1
class Type;

避免循环定义。

静态变量

类的静态变量只能在类外初始化,类中只能初始化 static const int 类型,去掉 const 或者不使用 int 类型均会报错。

  • static const int:允许类内初始化。
  • static constexpr:必须类内初始化,不能只声明。
  • static 关键字的其它类型:不允许类内初始化,仅可以在类内声明。

据说是为了避免每个对象中都包含该静态成员变量。

1
2
3
4
5
class Type {
public:
static const float a;
};
const float Type::a = 1.0;// 怪诶,这种写法

注:constexpr 将尽量在编译阶段运算。

虚函数(virtual)

调用成员函数时,虚函数由指针指向的实际类型决定,普通函数由指针类型决定。

简单来说就是普通成员函数都放在代码区,取的时候按照指针类型去取;而虚函数在对象中保存虚函数表指针 vptr,通过 vptr 找到虚函数表 vtbl,再通过 vtbl 找到对应的虚函数,因此由对象的实际类型决定。(包含虚函数的类占用的空间会更大)

构造函数不能是虚函数,而析构函数最好设置为虚函数(析构所有的新成员)。

(纯虚函数 virtual void func()=0; 用于抽象类,即抽象函数)
内联函数

inline 关键字,类内实现的函数都默认内联,类外实现的函数须加关键字实现内联且不能放在代码文件中(需要放在头文件中)。

【内联函数的一些问题】待填坑。

访问权限

没有指定访问权限的成员变量默认私有。

  • public:公开
  • protected:子类可访问
  • private:仅自己或友元可访问
常成员函数

写法:void func() const {}

  • 普通函数的 this 相当于 T* const this:指针不可变,指向内容可变。
  • 常成员函数的 this 相当于 const T* const this:指针不可变,指向内容不可变。

如果对象是 const 类型的话,不修改对象的成员函数若不指定为 const 函数则无法调用。

类函数/类变量

与 Java 中类似,加 static 关键字,最好使用 ClassName::FuncName() 而不是 ObjectName.FuncName() 调用。

  • 没有 this 指针。
  • 没有 const 修饰。(没有对象谈何对对象不做改动)

构造函数与析构函数

explicit 关键字

不允许奇怪的隐式类型转换,像 Point p = 1 这种。

一些无聊的尝试

定义 static 类函数,使用引用 & 或指针返回创建的对象(一种是 new 到堆里,一种是 static 到静态数据区【这种方法只能创建一次】)。

构造函数

当类有带参数的构造函数时,不能不初始化(ClassName object;)。

构造函数其实是在对象创建之后才调用的。

初始化

类变量只有整型常量可以初始化,这个是前面提到过的 static const int

在参数列表里初始化引用对象和常量,这样是没问题的:

1
2
3
4
5
int val = 1;
class Type {
int &a; const int b;
Type(): a(val), b(1) {}
};

初始化列表将严格按照定义的顺序初始化。

初始化列表先于构造函数的代码块执行。

析构函数

没有参数:~ClassName() {}

析构函数则是在对象销毁之前调用的。

拷贝和赋值

返回值存放在栈中,使用时再拷贝出来。(返回的不是单个整数时编译为何种汇编语言?实验没有成功进行因为被编译器所优化)

拷贝构造函数

默认存在。

调用:

  • Type b(a);
  • Type b = a;(好像不太推荐)

自定义(需要引用符号,引用传递):

  • 不修改被拷贝的对象:Type(const Type &rhs) {}
  • 要转移被拷贝的对象:Type(Type &rhs) {}

参数列表调用:

  • 这样调用的是拷贝构造函数(无论加不加引用符号):Type(Type &a):a(a) {}
  • 这样调用的是普通构造函数:Type(int a):a(a) {}

【为什么拷贝构造函数不能值传递?】值传递时会在函数执行时创建局部变量,该局部变量初始化时需要使用拷贝构造函数将实际参数的值赋给它,这样再次调用拷贝构造函数就会产生死循环。

浅拷贝/深拷贝

浅拷贝:成员变量是一个对象时调用其对应的拷贝构造函数,其余(基本数据类型,指针类型,引用类型)直接按二进制拷贝。

深拷贝:自己实现,拷贝的不再是指针,而是指针内的值。

有构造函数 无构造函数
有拷贝构造函数 无默认构造函数和拷贝构造函数 无默认构造函数和拷贝构造函数
无拷贝构造函数 有拷贝构造函数 有默认构造函数和拷贝构造函数

即没有什么提供什么,但自定义的拷贝构造函数就可以代替构造函数

禁止拷贝
  • 自定义一个没有实现 {}private 的拷贝构造函数。
  • C++1z:T(const T& t)=delete
拷贝与赋值
  • 拷贝:Type a = b;
  • 赋值:Type a; a = b;

【有引用类型的成员不能赋值,可以拷贝】拷贝是初始化的过程,而赋值时引用类型的成员已经有引用的变量,因其不能更改所以不能赋值。(成员变量为引用类型:须自定义构造函数和拷贝构造函数,不能赋值。)

自定义赋值函数:重载 = 运算符 Type &operator = (const Type &rhs) { return *this; }(参考内置类型的使用习惯)。

课件中一个有趣的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <cstdio>
class Type {
public:
int *p;
Type() { p = new int(1024); }
Type &operator = (const Type &rhs) {
delete p;
p = new int(*rhs.p);
return *this;
}
};
int main() {
Type a; a = a;
Type &b = a; a = b;
return 0;
}

此时编译正常,但逻辑错误,应当判断 (&rhs != this)再进行删除和赋值。

运算符重载

只能重载一元运算符和二元运算符:,&(与运算)、->newnew[] 等。

两种形式:自由函数 AND 成员函数。

访问权限等参考内置函数,如:

  • T &operator *= (const &T rhs) {}
  • T operator * (const &T rhs) {}
  • bool operator ! () {}
  • T &operator ++ () {}:对应 ++t,返回的是自加后的值,因此要加 & 符号。
  • T operator ++ (int) {}:对应 t++,返回的是自加前的值,因此无需 & 符号。
  • int opeartor [] (int index) const { return arr[index]; }
  • int &operator [] (int index) { return arr[index]; }
  • int operator () () const { return 1024; }:仿函数,可用 lambda 表达式代替。
  • 重载 -> 运算符须保证:返回指针类型或返回的自定义类型重载了 ->
  • ostream &operator << (ostream &out, const T &t) { out << t.a; return out; }
  • istream &operator >> (istream &in, const T &t) { in >> t.a; return in; }

注:lhsrhs 分别是 Left Hand Side 和 Right Hand Side 的缩写。

左操作数是自定义类型的尽量使用成员函数的形式重载,不要重载运算符 &&||,

动态内存管理

动态内存管理:存放在全局堆区(new 申请的内存)

new/delete

分配数组类型:int *p = new int(5)

分配指针类型:const T **p = new P*();

释放数组类型:delete[] arr;

new 失败后会跳转 new_error_handle() 函数(可通过 set_new_handler() 修改),成功会执行构造函数并返回对应类型的指针(重载 new 运算符返回的 void * 类型指针会强制转化为 T * 类型指针)。

1
2
3
4
5
6
7
8
9
#include <cstdio>
#include <new>
void func1() { puts("erorooor"); }
int main() {
typedef void (*func)();
func before = std::set_new_handler(func1);
std::set_new_handler(before);
return 0;
}
重载 new/delete

void* operator new(std::size_t); 对应 new T(1024);

void* operator new(std::size_t, const string &s); 对应 new("string") T(1024);

void operator delete(void *, std::set_t); 对应 delete p;

动态数组
1
2
int* arr = new int[n];// n 可以为变量
delete[] arr;

普通类型对象数组 Type arr[1024]; 要求必须有无参构造函数。

指针类型对象数组 Type* arr[1024]; 不要求必须有无参构造函数。

auto_ptr

将指针封装在类中,以便析构时自动释放。

shared_ptr

共享指针,记录指针被引用的次数。

  • <memory>
  • std::shared_ptr<Type>
  • std::make_shared<Type>()
  • p.get()
  • *p 等价于 *p.get()
  • p.use_count()
  • p.reset()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <cstdio>
#include <memory>
int main() {
std::shared_ptr<int> p = std::make_shared<int>();
*p = 1024;
printf("[p]\tptr: %p val: %d use_count: %ld\n", p.get(), *p, p.use_count());
std::shared_ptr<int> p0(p);
printf("[p]\tptr: %p val: %d use_count: %ld\n", p.get(), *p, p.use_count());
printf("[p0]\tptr: %p val: %d use_count: %ld\n", p0.get(), *p0, p0.use_count());
p.reset();
printf("[p0]\tptr: %p val: %d use_count: %ld\n", p0.get(), *p0, p0.use_count());
return 0;
}
/*
[p] ptr: 0x6000025c4058 val: 1024 use_count: 1
[p] ptr: 0x6000025c4058 val: 1024 use_count: 2
[p0] ptr: 0x6000025c4058 val: 1024 use_count: 2
[p0] ptr: 0x6000025c4058 val: 1024 use_count: 1
*/

printf 中输出地址用 %p

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <cstdio>
#include <memory>
class Point {
public:
int x, y;
Point(int x, int y): x(x), y(y) {}
};
int main() {
std::shared_ptr<Point> p = std::make_shared<Point>(12, 4);
printf("[p]\tptr: %p val: (%d, %d) use_count: %ld\n", p.get(), p->x, p->y, p.use_count());
std::shared_ptr<Point> p0 = p;
printf("[p]\tptr: %p val: (%d, %d) use_count: %ld\n", p.get(), p->x, p->y, p.use_count());
printf("[p0]\tptr: %p val: (%d, %d) use_count: %ld\n", p0.get(), p0->x, p0->y, p0.use_count());
p->x = 3; p->y = 9; p = NULL;
printf("[p0]\tptr: %p val: (%d, %d) use_count: %ld\n", p0.get(), p0->x, p0->y, p0.use_count());
return 0;
}
/*
[p] ptr: 0x600003bad1d8 val: (12, 4) use_count: 1
[p] ptr: 0x600003bad1d8 val: (12, 4) use_count: 2
[p0] ptr: 0x600003bad1d8 val: (12, 4) use_count: 2
[p0] ptr: 0x600003bad1d8 val: (3, 9) use_count: 1
*/

可以像普通指针一样用赋值号,也会被累计其中哦!

函数体结束就释放了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <cstdio>
#include <memory>
class Point {
public:
int x, y;
Point(int x, int y): x(x), y(y) {}
};
Point *addr;
void create() {
std::shared_ptr<Point> p = std::make_shared<Point>(3, 5);
printf("%p %d %d\n", p.get(), p->x, p->y);
addr = p.get();
}
int main() {
create();
printf("%d %d", addr->x, addr->y);
return 0;
}
/*
0x600001365118 3 5
0 0
*/

除非作为返回值:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <cstdio>
#include <memory>
class Point {
public:
int x, y;
Point(int x, int y): x(x), y(y) {}
};
Point *addr;
std::shared_ptr<Point> create() {
std::shared_ptr<Point> p = std::make_shared<Point>(3, 5);
printf("%p %d %d\n", p.get(), p->x, p->y); addr = p.get();
return p;
}
int main() {
std::shared_ptr<Point> && pp = create();
printf("%p used_count: %ld\n", pp.get(), pp.use_count());
printf("%d %d", addr->x, addr->y);
return 0;
}
/*
0x6000025f51d8 3 5
0x6000025f51d8 used_count: 1
3 5
*/

这个东西可以利用前面学到的拷贝构造函数和重载赋值自己实现。

乱七八糟概念

悬浮指针:释放后访问指针。

内存泄漏:申请了没释放。

写时复制:指向多个同一资源,需要修改的时候才复制一份分开,用来节省空间(有点像可持久化)。

定位分配:分配到已有空间,就是下面这个东西(需要 <new>,在后面指定分配到的地址)。

1
2
3
4
5
6
7
8
9
10
11
12
#include <cstdio>
#include <new>
int main() {
char buf[1024];
char* s = new(buf) char(32);
char* ss = new(buf + 32) char(32);
printf("%p %p %p\n", buf, s, ss);
return 0;
}
/*
0x16d762cf8 0x16d762cf8 0x16d762d18
*/

转换函数、命名空间、友元、嵌套类、流

类型转换

基本数据类型2自定义类型:构造函数(可通过 explicit 禁止)

自定义类型2基本数据类型:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <cstdio>
#include <cmath>
class Point {
public:
int x, y;
Point(int x, int y): x(x), y(y) {}
explicit operator double() const {
return sqrt(x * x + y * y);
}
};
int main() {
Point p(3, 4);
printf("%lf\n", double(p));
return 0;
}
/*
5.000000
*/
/*
去掉 explicit 可以有这样的写法:
double q = p;
而非:
double q = (double)p;
*/

(C 风格强制类型转换 (Type)Value 和 C++ 风格强制类型转换 Type(Value) 应该没什么大的区别)

命名空间

耳熟能详的东西:namespace NAME {}

没见过的东西::: 单独出现表示全局命名空间,否则表示当前命名空间。

可以嵌套:namespace A { namespace B {} }

可以起别名:namespace A = B;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <cstdio>
namespace A {
namespace B {
int value = 32;
}
}
namespace C = A;
int value = 1024;
int main() {
int value = 64;
printf("%d %d %d\n", value, ::value, C::B::value);
return 0;
}

/*
64 1024 32
*/

static 访问域:仅能在当前文件中被访问。

奇怪的东西:匿名空间 namespace {},看看它怎么回事。

1
2
3
4
5
6
7
8
9
10
#include <cstdio>
namespace { int value = 32; }
int main() {
int value = 64;
printf("%d %d\n", value, ::value);
return 0;
}
/*
64 32
*/

如果有了全局变量,优先用全局变量

1
2
3
4
5
6
7
8
9
10
11
#include <cstdio>
int value = 1024;
namespace { int value = 32; }
int main() {
int value = 64;
printf("%d %d\n", value, ::value);
return 0;
}
/*
64 1024
*/

对于匿名空间,编译器会给它一个名字并且自动 using namespace XXX;,它和 static 具有相同的 internal 链接属性,只对本文件可见,C++ 更提倡使用匿名空间而不是 static

using 关键字用于汇入当前命名空间。

嵌套类

嵌套类亦有所谓的访问权限(publicprivate)。

注:没有指定访问权限的成员变量默认私有。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class A {
int a;
class B {
int b;
void func(A a) {
printf("%d %d %d\n", a.a, a.pub_a, a.pri_a);
}
public:
int pub_b;
private:
int pri_b;
};
void func(B b) {
// error: 'b' is a private member of 'A::B'
// printf("%d %d %d\n", b.b, b.pub_b, b.pri_b);
printf("%d\n", b.pub_b);
}
public:
int pub_a;
private:
int pri_a;
};
/*
内部类可以访问外部类的私有成员变量。
外不能不能访问内部类的私有成员变量。
*/
友元 friend

友元函数/友元类:允许访问类的私有成员,写在类的开始,不指明访问权限。

一种很不优雅的妥协,friend关键字只能用在类中,表明允许其后的函数/类访问该类的私有元素,这个函数/类不在当前类中,只需照常使用,额外增加了访问权限而已。

字节流/字符流/文件流,istreamostream

类间关系

强关联/硬关联/弱关联/软关联:???

联系强弱:(另一类作为)成员变量 > 函数参数和返回值 > 函数实现(局部变量)

关联关系

一般关联/自关联/关联类(设定一个类)/聚集关联

关联关系:聚合关系(不负责创建和销毁)/组合关系(负责创建和销毁)

依赖关系

补充:UML图
  • +(public)、-(private)、#(protected)
  • 抽象类和抽象方法用斜体
  • 接口 <<interface>> 下面是接口名。
  • 类实现接口:箭头(空心三角和虚线)
  • 类继承父类:(子类向父类)箭头(空心三角和实线)
  • 关联关系(成员变量是一个类):(指向成员变量的类)箭头(角和实线)
  • 依赖关系(成员函数的参数/返回值是一个类):(指向用到的类)箭头(角和虚线)
  • 聚合关系 has-a:关联关系的特例,箭头(空心菱形和实线角)
  • 组合关系 contain-a:关联关系的特例,箭头(实心菱形和实线角)

类的设计

继承/封装/多态

依赖(参数或返回类型)/关联(成员变量)

继承

可以多继承,不继承父类的构造函数、析构函数、拷贝构造函数、赋值函数、类型转换函数,但在构造函数执行前会先执行父类的构造函数。

继承方式:public、protected、private(默认),继承后的访问权限根据继承方式进行相应的削弱(如 protected 继承方式将父类中 public 成员削弱为 protected)。

【private 继承为什么还要存在?】待填坑。

如何调用父类的构造函数?在初始化列表中调用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <cstdio>
class P {
protected:
int x, y;
P(int x = 3, int y = 4): x(x), y(y) {}
};
class V: public P {
int z;
public:
V(int z): P(1, 2), z(z) {}// Here
void get() { printf("%d %d %d", x, y, z); }
};
int main() {
V v0(3); v0.get();
return 0;
}

为什么 C++ 中没有 super?因为 C++ 支持多继承啊,super 指的是哪个呢?

再给个例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <cstdio>
#include <cmath>
class P {
public:
int x, y;
P(int x = 3, int y = 4): x(x), y(y) { puts("P(int x = 3, int y = 4): x(x), y(y)"); }
P(const P &rhs) { puts("P(const P &rhs)"); x = rhs.x + 1; y = rhs.y + 1; }
P &operator = (const P &rhs) {
puts("P &operator = (const P &rhs)");
if(&rhs != this) {
x = 0; y = 0;
x = rhs.x - 1; y = rhs.y - 1;
}
return *this;
}
operator double() { return sqrt(x * x + y * y);}
};
class V: public P {
int z;
public:
V(int z): P(1, 2), z(z) { puts("V(int z): P(1, 2), z(z)"); }
void get() { printf("%d %d %d\n", x, y, z); }
V(const V &rhs): P(rhs) { puts("V(const V &rhs)"); z = rhs.z + 1; }
V &operator = (const V &rhs) {
puts("V &operator = (const V &rhs)");
if(&rhs != this) {
P::operator=(rhs);
z = 0; z = rhs.z - 1;
}
return *this;
}
};
int main() {
V v0(3); v0.get();
V v1 = v0; v1.get();
v1 = v0; v1.get();
printf("%lf\n", double(v1));
return 0;
}
/*
P(int x = 3, int y = 4): x(x), y(y)
V(int z): P(1, 2), z(z)
1 2 3
P(const P &rhs)
V(const V &rhs)
2 3 4
V &operator = (const V &rhs)
P &operator = (const P &rhs)
0 1 2
1.000000
*/

注意代码中 V 类型的 rhs 可以调用 P(rhs) 以及 P::operator=(rhs)

注意这里类型转换函数直接调用了父类的。

子类的一系列操作
  • newdefine:子类中定义,父类中没有。
  • redefine:子类中定义,父类中也有。
  • overload:子类中定义多个,重载。
  • overwrite:子类中定义,父类中有同名的,被隐藏
  • override(和我之前以为的 override 不太一样):子类中定义,父类中为同名虚函数。

继承和类型转换

protected/private 向上(子类转父类)转换:先取地址再转对应类型的指针,不推荐。

public 向上转换:安全的,会创建一个新的对象,所以尽可能使用指针或引用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <cstdio>
class P {
public:
int x, y;
P(int x = 3, int y = 4): x(x), y(y) {}
};
class V: public P {
int z;
public:
V(int z): P(1, 2), z(z) {}
};
int main() {
V* v0 = new V(3);
P* p0 = v0;
printf("%p %p\n", v0, p0);
return 0;
}
/*
0x600001de4040 0x600001de4040
*/

public 向下转换:可能出错。

乱七八糟的类型转换操作符

static_cast<T>(exp)

const_cast<T>(exp):修改 constvolatile 约束。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <cstdio>
class P {
public:
int x, y;
P(int x = 3, int y = 4): x(x), y(y) {}
void query() const {
printf("%d %d\n", x, y);
P* p = const_cast<P*>(this);
p->x = 1; p->y = 2;
}
};
int main() {
P p0; p0.query(); printf("%d %d\n", p0.x, p0.y);
return 0;
}
/*
3 4
1 2
*/

然后尽管这个函数是 const,这个对象的值还是被改了。

reinterpret_cast<T>(exp):重新解释,用于函数。

dynamic_cast<T>(exp):用于多继承时父类转子类。

%%%多重继承

顺序:构造顺序相同,析构顺序相反。

菱形结构多重继承产生重复:虚继承(继承类加 virtual 关键字修饰),被继承的类称为虚基类。

工具类

放置静态函数和静态变量。

接口类

C++ 没有 interface,通过纯虚函数(抽象函数)实现,指定 virtual 之外还要制定函数 =0

虚机制

静态编联与动态编联

静态编联:编译期间决定调用关系。

动态编联:执行期间决定调用关系,虚机制是实现方式之一。

虚指针

我以为是个新东西,其实就是 vptr。

虚函数

声明时需要加 virtual,定义时可加可不加。

  • 静态函数显然不能是虚函数。
  • 如果有虚函数,析构函数应当是虚函数。
  • 构造函数和拷贝构造函数不能是虚函数,赋值函数通常不定义为虚函数。

【为什么构造函数不能是虚函数?】调用构造函数需要 vptr,此时对象还没有实例化,找不到 vptr。

【为什么赋值函数不要定义为虚函数?】因为子类不继承父类的赋值函数(参数类型不同)。

继承
  • public 继承,且会继承父类的虚函数。
  • 重写函数(函数名相同,多数要求参数相同,返回类型相同或相容)默认 virtual 可省略(包括析构函数)。
虚函数表
  • 一个类只有一个(实例化首个对象时创建),所有对象共享。
  • 父子类的虚拟表中相同函数的位置相同。
静态类型与动态类型

静态类型:编译期间确定的类型(对应指针的类型)。

动态类型:执行期间确定的类型(对应指针指向的实际类型)。

【重要】如何执行?
  • 在静态类型(定义的指针类型)中寻找对应函数,找不到编译错误。
  • 若找到的函数不是 virtual 函数则 p->P::func(),若为 virtual 函数则根据 vptr 执行 (*p->vptr)[index]((void *)p, ...)

在构造函数中调用虚函数当然调用的对应类自己的那个函数,由于 B 类继承自 A 类,构造 B 类时也会构造 A 类(注意这个 A 类仅用于创建的 B 类对象,每再创建一个 B 类会就会再执行一次 A 类的构造函数。

B 类对象构造的过程:

  • 构造 A 类
  • 构造 B 类

B 类对象析构的过程:

  • 析构 B 类
  • 析构 A 类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
A 的析构函数不加 virtual 会在运行时出错哦,因为 object 调用的是 A 的析构函数,导致释放了两次。
*/
#include <cstdio>
class A {
public:
int variable;
A(int variable): variable(variable) { get(); }
virtual ~A() { get(); }
virtual int get() {
printf("[A] get();\n");
return variable;
}
};
class B: public A {
public:
B(): A(1024) { get(); }
~B() { get(); }
int get() {
printf("[B] get();\n");
return variable;
}
};
int main() {
A* object = new B();
A* object2 = new B();
delete object;
delete object2;
return 0;
}
/*
[A] get();
[B] get();
[A] get();
[B] get();
[B] get();
[A] get();
[B] get();
[A] get();
*/
私有的虚函数访问

指针是父类类型时,在父类中调用一个子类中重写过的虚函数(父类中当然定义了),实际上调用的是子类中的那个版本,即便它是 private 的,依然能够执行。(跨类访问私有函数)

具体类和抽象类

抽象类:有纯虚函数(virtual 函数 =0),也叫抽象函数。

具体类和抽象类的子类既可以是具体类也可以是抽象类。

纯抽象类:除了静态、构造、析构函数均为纯虚函数。

接口类:纯抽象类,成员均为 public static

【!】纯虚函数可以在类外给出定义(当然可以不定义),例如:

1
2
3
4
5
6
7
8
9
10
#include <cstdio>
class A {
public:
int variable;
A(int variable): variable(variable) {}
virtual void abstractFunction() const = 0;
};
void A::abstractFunction() const {
puts("?");
}

当然,抽象类不能被实例化,所以个人感觉没什么意义。

继承自抽象类后没有实现对应的抽象函数子类就还是抽象类。

其它

typeid(exp_or_type):类型比较。

dynamic_cast<T>(exp):父类转子类,T 为指针类型失败返回 nullptr,T 为引用类型失败产生异常。

关于虚函数一些例子
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/* 父类定义为虚函数后,子类重写时 virtual 关键字可省略 */
#include <cstdio>
class A {
public:
int variable;
A(int variable): variable(variable) {}
virtual int get() {
printf("[A] get();\n");
return variable;
}
};
class B: public A {
public:
B(): A(1024) {}
int get() {
printf("[B] get();\n");
return variable;
}
};
int main() {
A* object = new B();
object->get();
return 0;
}
/*
[B] get();
*/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/* 仅仅定义子类重写的函数是虚函数无效 */
#include <cstdio>
class A {
public:
int variable;
A(int variable): variable(variable) {}
int get() {
printf("[A] get();\n");
return variable;
}
};
class B: public A {
public:
B(): A(1024) {}
virtual int get() {
printf("[B] get();\n");
return variable;
}
};
int main() {
A* object = new B();
object->get();
return 0;
}
/*
[A] get();
*/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/* 父类类型的指针不能用于访问子类中特有的函数 */
#include <cstdio>
class A {
public:
int variable;
A(int variable): variable(variable) {}
virtual int get() {
printf("[A] get();\n");
return variable;
}
};
class B: public A {
public:
B(): A(1024) {}
int getB() {
printf("[B] get();\n");
return variable;
}
};
int main() {
A* object = new B();
object->getB();
return 0;
}
/*
error: no member named 'getB' in 'A'; did you mean 'get'?
*/
其它

越想越觉得虚函数和抽象函数并不是完全没有联系,virtual 关键字就好像告诉我们尽可能地不使用当前类的代码而去使用子类的实现,不同之处在于抽象函数不允许子类没实现而虚函数给了一个子类没实现时的保底。

多态性

定义:相同的消息请求,执行不同的代码体,产生不同的结果。

静态多态:根据静态类型确定执行的代码,如模版函数重载

动态多态:根据目标对象动态类型和参数表静态类型确定执行的代码,如虚机制

使用虚机制

面向对象编程,使用父类指针执行子类代码。

拷贝构造

拷贝构造函数不能是虚函数,静态类型和动态类型不同,如何复制呢?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
定义虚函数 clone()
*/
#include <cstdio>
class A {
public:
int a;
A(int a = 32): a(a) {}
virtual A* clone() { puts("clone A"); return new A(*this); }
};
class B: public A {
public:
int b;
B(int b = 64): b(b) {}
B* clone() { puts("clone B"); return new B(*this); }
};
int main() {
A* t = new B();
A* t0 = t->clone();
return 0;
}
/*
clone B
*/
问题

如果有多个函数,每个函数有若干种实现,则子类个数为它们相乘。

【解决方案】在类中定义三个变量指向三个新类,每个新类定义一个虚函数,通过继承该类实现这个函数,调用函数时调用对应成员变量的成员函数即可,这样子类个数为各个函数实现方案数的和。

面向对象程序设计

懒得看,鸵鸟法可知这一章没什么东西(doge

一些好玩的

【开玩笑的】隐藏运算符:趋向于(雾):

1
2
int t; scanf("%d", &t);
while(t-->0) {}

参考资料

1
2
3
4
5
6
7
8
9
#include <cstdio>
int func(int a, long long int = 2) {
int b = *((char*)(&a) - 12);
return b;
}
int main() {
printf("%d\n", func(1));
return 0;
}

函数省略形式参数名:https://blog.csdn.net/C0631xjn_/article/details/127280342

类中静态变量初始化:https://blog.csdn.net/qq_50868258/article/details/123139071

类中静态变量初始化:https://blog.csdn.net/sevenjoin/article/details/81772792

虚函数:https://zhuanlan.zhihu.com/p/28530472

explicit 关键字与离谱的 C++:https://zhuanlan.zhihu.com/p/52152355

shared_ptr:https://zhuanlan.zhihu.com/p/547647844

UML:https://zhuanlan.zhihu.com/p/109655171

虚继承内存空间安排:https://blog.csdn.net/SuLiJuan66/article/details/48897867

虚继承:https://zhuanlan.zhihu.com/p/104344453

待补充

private构造函数

重载区分

拷贝构造函数的循环