c++ 关键字 auto decltype typeid

2021/7/31 14:06:14

本文主要是介绍c++ 关键字 auto decltype typeid,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

简明描述三者概念

auto:自动类型推导,声明变量时必须赋初值。类型由右值的决定
decltype :声明表达式类型,声明变量时时不必赋初值。类型由编译器根据表达式自动推导
typeid:运行时类型信息(RTTi),不能用来声明变量

auto 和 decltype都是编译时就确定的类型,typeid更像是一个返回类型信息的函数。因不是实际类型,故不能用来声明变量。使用时需包含头文件。

先上代码:

	int a = 0;
	auto b = a;
	decltype(b) c;
	if ((typeid(a) == typeid(b))
		&& (typeid(a) == typeid(c))
		&& (typeid(b) == typeid(c))) { //true

		cout << "true" << endl;
	}

可以看到a、b、c的类型完全相同,都是int类型
在这里插入图片描述
补充两条

int fun() {
	cout << "fun is run" << endl;
	return 0;
}
int c;
decltype(( b )) d = c;//( b )表示b的引用,故c的类型为int &
decltype(fun) *f = fun; //操作数为函数时,decltype(fun)为函数类型,多用来定义函数指针
f(); //"fun is run" 

在发生继承时的使用

定义两个类Base、Derive,使Derive继承Base。

class Base {
public:
	int fun() {
		return 0;
	}
	virtual int vir_fun1() {
		return 0;
	}
};
class Derive : public Base {
public:
	int fun() {
		return 0;
	}
	virtual int vir_fun1() override /* override: 表示该函数必须重写基类函数*/ {
		return 0;
	}
};

定义如下变量

Base Ba;
Derive Da;
Base* pBa = &Da;
Base& Bb = Da;

typeid的效果

if (typeid(pBa) == typeid(Derive*)) { //false
	cout << "type of pBa is Derive*" << endl;
}
else if (typeid(pBa) == typeid(Base*)) {//true
	cout << "type of pBa is Base*" << endl;
}
if (typeid(*pBa) == typeid(Derive)) { //true 基类指针转化为子类
	cout << "type of *pBa is Derive" << endl;
}
if (typeid(Bb) == typeid(Derive)) { //true 基类引用转化为子类
	cout << "type of Bb is Derive" << endl;
}

注意,这种动态类型转化只有当父类中存在虚函数时才能成功。若父类中不存在虚函数是不会发生的

class Base {
public:
	int fun() {
		return 0;
	}
//	virtual int vir_fun1() {
//		return 0;
//	}
};
class Derive : public Base {
public:
	int fun() {
		return 0;
	}
//	virtual int vir_fun1() override /* override: 表示该函数必须重写基类函数*/ {
//		return 0;
//	}
};

再看效果

Base Ba;
Derive Da;
Base* pBa = &Da;
Base& Bb = Da;
if (typeid(pBa) == typeid(Derive*)) { //false
	cout << "type of pBa is Derive*" << endl;
}
else if (typeid(pBa) == typeid(Base*)) {//true
	cout << "type of pBa is Base*" << endl;
}
if (typeid(*pBa) == typeid(Derive)) { //false
	cout << "type of *pBa is Derive" << endl;
}

if (typeid(Bb) == typeid(Derive)) { //false
	cout << "type of Bb is Derive" << endl;
}

auto 及 decltype 在发生继承时的效果

auto autoBc = Bb; //Base 会把引用类型转化为实际类型
auto autoBd = pBa; //Base*
decltype(Bb) Bd = Bb;//Base & 不会把引用类型转化为实际类型
decltype(autoBc) Be = autoBc;//Base

if (typeid(autoBc) == typeid(Base)) { //true
	cout << "type of autoBc is Base" << endl;
}
if (typeid(autoBd) == typeid(Base *)) { //true
	cout << "type of autoBd is Base*" << endl;
}
if (typeid(Bd) == typeid(Base &)) { //true 
	cout << "type of Bd is Base &" << endl;
}
if (typeid(Be) == typeid(Base)) { //true
	cout << "type of Be is Base" << endl;
}

不得不提的dynamic_cast

跟RTTi相关的不仅有typeid,还有一个类型转换关键字dynamic_cast,还是代码描述

	Base* pBb = &Ba;
	if(dynamic_cast<Derive *>(pBa) != nullptr) { //true
		cout << "pBa dynamic_cast to DerivePtr success" << endl;
	}
	if (dynamic_cast<Derive*>(pBb) == nullptr) { //true
		cout << "pBa dynamic_cast to DerivePtr fault" << endl;
	}

可见当父类指针指向子类对象时,父类指针才能成功动态转化为子类指针,否则转换失败,结果为nullptr,同样,当父类中不存在虚函数时会导致编译失败。

此文仅为本人学习c++过程中的笔记,分享处理供大家探讨,不应作为技术参考。如发现不对的地方还望不吝赐教。
谢过



这篇关于c++ 关键字 auto decltype typeid的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程