ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

c++ 关键字 auto decltype typeid

2021-07-31 13:32:13  阅读:417  来源: 互联网

标签:typeid decltype cout int c++ Base return true


简明描述三者概念

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++过程中的笔记,分享处理供大家探讨,不应作为技术参考。如发现不对的地方还望不吝赐教。
谢过

标签:typeid,decltype,cout,int,c++,Base,return,true
来源: https://blog.csdn.net/LSX_YM/article/details/119272459

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有