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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值