b站Cherno的课[66]-[70]
- 一、C++的类型双关
- 二、C++的union(联合体、共用体)
- 三、C++的虚析构函数
- 四、C++的类型转换
- 五、条件与操作断点——VisualStudio小技巧
一、C++的类型双关
作用:在C++中绕过类型系统
C++是强类型语言
有一个类型系统,不是所有东西都用auto去声明
可以用auto,毕竟它也是一个关键字
但在JavaScript中,没有变量类型的概念
在创建变量时,不需要声明变量类型
当我们接受它作为函数的参数时,没有真正的类型系统
但C++有一个类型系统
当我们创建变量的时候,必须声明整数或双精度数或布尔数或结构体或者类等等
然而,这种类型系统并不像在其他语言中那样强制,比如java
它们的类型很难绕开,包括C#也是
你虽然也可以绕开类型系统,但要做更多的工作
在C++中,虽然类型是由编译器强制执行的,但您可以直接访问内存
这意味着在代码中一直使用这种类型,比如整数,但实际上,我现在要把这段内存,同样的内存,当作double类型,或者是class类型等
可以很容易地绕过类型系统
你是否要用,取决于你的实际情况
在某些情况下,您绝对不应该规避类型系统,因为类型系统存在是有原因的
#include <iostream>
int main()
{
int a = 50;
//double value = a; 隐式转换
double value = (double)a; // 显式转换
std::cout << value << std::endl;
std::cin.get();
}
#include <iostream>
int main()
{
int a = 50;
//double value = a; 隐式转换
//double value = (double)a; 显式转换
double& value = *(double*)&a;
value = 0.0;
std::cout << value << std::endl;
std::cin.get();
}
#include <iostream>
struct Entity {
int x, y;
};
int main()
{
Entity e = { 5, 8 };
// 回到了原始的内存操作
int* position = (int*)&e;
int y = *(int*)((char*)&e + 4);
std::cout << y << std::endl;
std::cin.get();
}
我们可以用不同的方式解析同一段内存,从而得到不同的结果,类型只是我们约定的解析内存的方式
类型双关:我要把我拥有的这段内存,当作不同类型的内存来对待
我们需要做的只是将该类型作为指针,然后将其转换为另一个指针
然后如果有必要,还可以对它进行解引用
二、C++的union(联合体、共用体)
联合体有点像类类型,或者结构体类型
只不过它一次只能占用一个成员的内存
这意思是说,通常如果我们有一个结构体,我们声明比如4个浮点数,
我们可以有4乘以4个字节在这个结构体中,总共是16个字节
因为我们有四个成员,而且很明显
当你不断向类或结构体中添加更多成员时,其大小会不断增长
一个联合体只能有一个成员
如果要声明四个浮点数 ABCD 联合体的大小仍然是4个字节,当改变ABCD的值的时候,内存是一样的
改变a设成5 b的值也是5
你可以像使用结构体或类一样使用它们
你也可以给它添加静态函数或者普通函数、方法等等
通常union是匿名使用的,但是匿名union不能含有成员函数
通常被用来做类型双关,union可读性更强
#include <iostream>
struct Vector2 {
float x, y;
};
struct Vector4 {
float x, y, z, w;
};
void PrintVector2(const Vector2& vector)
{
std::cout << vector.x << "," << vector.y << std::endl;
}
int main()
{
struct Union
{
union
{
float a;
int b;
};
};
Union u;
u.a = 2.0f;
std::cout << u.a << "," << u.b << std::endl;
}
#include <iostream>
struct Vector2 {
float x, y;
};
struct Vector4 {
union
{
// 匿名的,只是一种数据结构,并没有添加任何东西
struct
{
float x, y, z, w;
};
struct
{
Vector2 a, b;
};
};
};
void PrintVector2(const Vector2& vector)
{
std::cout << vector.x << "," << vector.y << std::endl;
}
int main()
{
Vector4 vector = { 1.0f,2.0f,3.0f,4.0f };
//vector.x = 2.0f;
PrintVector2(vector.a);
PrintVector2(vector.b);
vector.z = 500.0f;
std::cout << "--------------------------" << std::endl;
PrintVector2(vector.a);
PrintVector2(vector.b);
std::cin.get();
}
union里的成员会共享内存,分配的大小是按最大成员的sizeof, 视频里有两个成员,也就是那两个结构体,改变其中一个另外一个里面对应的也会改变. 如果是这两个成员是结构体struct{ int a,b} 和 int k , 如果k=2 ; 对应 a也=2 ,b不变; union我觉得在这种情况下很好用,就是用不同的结构表示同样的数据 ,那么你可以按照获取和修改他们的方式来定义你的 union结构 很方便
一个联合体的应用场景:开发弱类型语言。例如js,let a=2; 紧接着写a=“abc”;变量a在一个时间点只会是一种类型,那就可以定义一个联合体来表示变量的值。
三、C++的虚析构函数
复习:析构函数~(销毁对象) 虚函数virtual
析构函数:在销毁对象时运行,卸载变量,清理使用过的内存,同时适用于栈和堆分配的对象
虚函数:允许我们在子类中重写方法
虚析构函数:二者结合,对于处理多态问题非常重要
#include <iostream>
class Base
{
public:
Base() { std::cout << "Base Constructor\n"; }
~Base() { std::cout << "Base Destrcctor\n"; }
};
class Derived : public Base
{
public:
Derived() { std::cout << "Derived Constructor\n"; }
~Derived() { std::cout << "Derived Destrcctor\n"; }
};
int main()
{
Base* base = new Base();
delete base;
std::cout << "----------------\n" << std::endl;
Derived* derived = new Derived();
delete derived;
std::cin.get();
}
int main()
{
Base* base = new Base();
delete base;
std::cout << "----------------\n" << std::endl;
Derived* derived = new Derived();
delete derived;
std::cout << "----------------\n" << std::endl;
Base* poly = new Derived();
delete poly;
std::cin.get();
}
// 只有基类的析构函数被调用,派生类的的析构函数没有被调用
// 这样会导致内存泄露
虚析构函数不是覆写析构函数,而是加上一个析构函数
如果我把基类析构函数改为虚函数
它实际上会调用两个(析构函数)
它会先调用派生类析构函数,然后在层次结构中向上,调用基类析构函数
标记为virtual,意味着c++就会知道在层次结构下的这个方法可能被重写了
一定要确保声明析构函数是虚函数,如果你允许它有子类的话
!!!如果用基类指针来引用派生类对象,那么基类的析构函数必须是 virtual 的,否则 C++ 只会调用基类的析构函数,不会调用派生类的析构函数。
四、C++的类型转换
C++是强类型语言,意味着存在一个类型系统,并且类型是强制的
#include <iostream>
class Base
{
public:
Base() {}
~Base() {}
};
class Derived : public Base
{
public:
Derived() {}
~Derived() {}
};
class AnotherClass : public Base
{
public:
AnotherClass() {}
~AnotherClass() {}
};
int main()
{
// 隐式转换
//int a = 5;
//double value = a;
double value = 5.25;
//int a = value;
//int a = (int)value;
double a = value + 5.3;
std::cout << a << std::endl;
std::cin.get();
}
#include <iostream>
class Base
{
public:
Base() { }
~Base() { }
};
class Derived : public Base
{
public:
Derived() { }
~Derived() { }
};
class AnotherClass : public Base
{
public:
AnotherClass() {}
~AnotherClass() {}
};
int main()
{
// 隐式转换
//int a = 5;
//double value = a;
double value = 5.25;
//int a = value;
//int a = (int)value;
// C语言风格类型转换
double a = (int)value + 5.3;
std::cout << a << std::endl;
std::cin.get();
}
#include <iostream>
class Base
{
public:
Base() { }
virtual ~Base() { }
};
class Derived : public Base
{
public:
Derived() { }
~Derived() { }
};
class AnotherClass : public Base
{
public:
AnotherClass() {}
~AnotherClass() {}
};
int main()
{
Derived* derived = new Derived();
Base* base = derived;
Derived* ac = dynamic_cast<Derived*>(base);
std::cin.get();
}
C++风格 共四种主要的cast 类型转换操作符
一个是 static_cast,还有reinterpret_cast、dynamic_cast、const_cat
static_cast:静态类型转换
reinterpret_cast:把这段内存重新解释成别的东西
const_cat:移除或添加变量的const限定
dynamic_cast:很好的方法来查看是否转换成功,与运行时类型信息RTTI(runtime type information)紧密相关
regex正则表达式
五、条件与操作断点——VisualStudio小技巧
关于条件与操作(conditions and actions)应用在断点上
操作断点是允许我们采取某种动作
一般是在碰到断点时打印一些东西到控制台
两种类型的操作断点:
操作断点和条件断点