五十一、 C++的dynamic_cast
dynamic_cast是C++风格的类型转换,它更像一个函数,它不像编译时进行的类型转换,而是在运行时计算。dynamic_cast是专门用于继承层次结构进行的强制类型转换。若我们有一个类,它是另一个类的子类,想转换为基类型或者从基类型转换为派生类型,就可以使用dynamic_cast。如下代码:
#include<iostream>
class Entity
{
};
class Player: public Entity
{
};
class Enemy: public Entity
{
};
int main()
{
Player* player = new Player(); //Player既是实体也是玩家
//Entity* player = new Player(); //改成实体
Entity* e = player; //隐式转换
Player* p = e; //报错,因为要把它转换成不同的类型,而很有可能是Enemy。
Entity* e1 = new Enemy(); //它仍然是一个Entity实体,但这里是一个player玩家,但不知道是不是enemy敌人
Player* p = e1; //报错
Player* p = (Player*)e1; //必须向编译器保证它是一个player,但这里把enemy强制转换成player
std::cin.get();
}
若尝试做一些enemy没有而player独有的事情,比如访问特定的成员变量,只有player有而enemy没有,程序会崩溃因为e1潜在的类型是enemy。我们不使用原始类型转换而用static_cast是可以的;
Entity* e1 = new Enemy();
Player* p = static_cast<Player*>(e1);
若换成dynamic_cast,会告诉我们它需要一个多态类类型;
因为dynamic_cast只用于多态类类型,我们需要Entity类有一个虚函数表,这样就有了需要重写的东西,意味它是多态类型。我们可以再次使用动态类型转换,用player e或者是enemy e1;
#include<iostream>
class Entity
{
public:
virtual void PrintName() {}
};
class Player: public Entity
{
};
class Enemy: public Entity
{
};
int main()
{
Player* player = new Player();
Entity* e = player;
Entity* e1 = new Enemy();
Player* p = dynamic_cast<Player*>(e);
Enemy* q = dynamic_cast<Enemy*>(e1);
std::cin.get();
}
如果强制转换是有效的,那么它将返回我们想要的Player指针的值,但如果它无效,说明它不是我们声称的给定类型,那么它就会给NULL。
五十二、衡量代码性能
__debugbreak是一个Windows特有函数,它会break the compiler;
#include<iostream>
#include <memory>
#include<chrono>
class Timer//Timer来对一个scope进行计时:
{
public:
std::chrono::duration<float> duration;
Timer()
{
m_StartTimepoint = std::chrono::high_resolution_clock::now();
}
~Timer()
{
Stop();
}
void Stop()
{
auto endTimepoint = std::chrono::high_resolution_clock::now();
auto start = std::chrono::time_point_cast<std::chrono::microseconds>(m_StartTimepoint).time_since_epoch().count();
auto end = std::chrono::time_point_cast<std::chrono::microseconds>(endTimepoint).time_since_epoch().count();
auto duration = end - start;
double ms = duration * 0.001;
std::cout << duration << "us (" << ms << "ms)\n";
}
private:
std::chrono::time_point<std::chrono::high_resolution_clock> m_StartTimepoint;
};
int main()
{
int value = 0;
{
Timer timer;
for (int i = 0; i < 100000; i++)
{
value += 2;
}
}
std::cout << value << std::endl;
__debugbreak();//__debugbreak是一个Windows特有函数,它会break the compiler;
std::cin.get();
}
要在release模式去测试,这样更有意义。
五十三、结构化绑定
C++17引入的新特性,可以在将函数返回为tuple、pair、struct等结构时且赋值给另外变量的时候,直接得到成员。
#include <iostream>
#include <tuple>
using namespace std;
int main()
{
tuple<int, double, string> my_tuple(1, 2.3, "hbh");
int my_int;
double my_double;
string my_string;
cout << get<2>(my_tuple) << endl;
tie(my_int, my_double, my_string) = my_tuple;
cout << my_string << endl;
auto [x, y, z] = my_tuple;
cout << z << endl;
return 0;
}
我们可以用get访问,或是tie拆包,但是用结构化绑定的方法,直接 auto [x, y, z] = my_tuple; 实在是方便。
五十四、std::optional
在读取文件内容的时候,往往需要判断读取是否成功,常用的方法是传入一个引用变量或者判断返回的std::string是否为空,例如:
#inclode<iostream>
std::string ReadFileAsString(const std::string& filepath,bool& outSuccess)
{
std::ifstream stream(filepath)
if(stream)
{
std::string result;
//read file
stream.close();
outSuccess = true;
return result;
}
outSuccess = false;
return std::string();
}
C++17引入了一个更好的方法,std::optional,就如名字一样,是检测变量是否是present的:
#include<iostream>
#include<fstream>
#include<optional>
std::optional<std::string> ReadFileAsString(const std::string& filepath)
{
std::ifstream stream(filepath);
if (stream)
{
std::string result;
// read file
stream.close();
return result;
}
return {};
}
int main()
{
std::optional<std::string> data = ReadFileAsString("data.txt");
// 可以用has_value()来判断是否读取成功
if (data.has_value()) // 其实也可以直接写为 if (data)
{
std::cout<<"File read successfully!\n";
}
else
{
std::cout<<"File not found!\n";
}
// 也可以用value_or()来判断是否读取成功,如果不成功则返回我们写入的值
// 这里即,如果成功则返回data值,否则返回字符串"Not resprent"
std::string result = data.value_or("Not resprent");
std::cout<<result<<std::endl;
std::cin.get();
}
五十五、C++一个变量多种类型(std::variant)
C++17引入一种可以容纳多种类型变量的结构,std::variant;
#include <iostream>
#include <variant>
int main()
{
std::variant<std::string, int> data; // <>里面的类型不能重复
data = "hbh";
// 索引的第一种方式:std::get,但是要与上一次赋值类型相同,不然会报错
// 另有data.index()方法,由于前面已经赋值data = "hbh",因此会返回0,默认也为0
std::cout << data.index() << std::endl;
std::cout << std::get<std::string>(data) << std::endl;
// 索引的第二种方式,std::get_if,传入地址,返回为指针
if (auto value = std::get_if<std::string>(&data))
{
std::string& v = *value;
}
data = 2;
std::cout << std::get<int>(data) << std::endl;
std::cin.get();
}
std::variant的大小是<>里面的大小之和(不过后来我自己测试,发现并不是这样,具体大小我也没弄清楚),与union不一样,union的大小是类型的大小最大值。虽然variant似乎更浪费内存,但是人家type safe,因此应该用它除非真的那么节省。----内容来自大佬
五十六、std::any
std::any,也是C++17引入的可以存储多种类型变量的结构,其本质是一个union,但是不像std::variant那样需要列出类型:(一般用不到)
#include <iostream>
#include <any>
// 此处写一个new的函数,是为了断点,看主函数里面哪里调用了new,来看其堆栈
void* operator new(size_t size)
{
return malloc(size);
}
int main()
{
std::any data;
data = 2;
data = std::string("hbh");
// 注意,左边是引用,那么右边的cast也要是引用来对应
std::string& s = std::any_cast<std::string&>(data);
std::cout << s << std::endl;
std::cin.get();
}
五十七、多线程处理(std::async)
std::async()是一个接受回调(函数或函数对象)作为参数的函数模板,并有可能异步执行它们。
Over,到此基本知识学习完毕,优化等现在还用不到。