在 C++ 中查找对象类型
本文讨论在 C++ 中查找对象类型的不同方法。
在 C++ 中查找类对象的类型
与简单变量的数据类型(如 int、bool 等)类似,类对象也有一个类型,即它们所属的类:
#include<iostream>
using namespace std;
class Vehicles
{
public:
string make;
string model;
string year;
};
int main()
{
Vehicles car;
}
在上面的例子中,main()
中定义的对象car的类型是Vehicles类,因为car是Vehicles的一个对象。
上面的例子非常简单,我们可以轻松识别对象类型。 但是,在更复杂的项目中,特别是在多个类和对象以及多个程序员一起工作的情况下,跟踪对象类型可能会变得非常混乱。
为了防止这个问题,我们可以使用一些方法来确定对象的类型,我们将在下面的示例中详细介绍它们。
使用 typeid().name() 查找 C++ 中类对象的类型
typeid()
是 typeinfo 库中包含的方法。 typeid.().name()
函数接受变量或类对象并以字符串形式返回其类型的名称。
typeid.().name()
函数的使用演示如下:
#include<iostream>
#include<typeinfo>
using namespace std;
class Vehicles
{
public:
string make;
string model;
string year;
};
class Aircraft
{
public:
string make;
string model;
string year;
};
int main()
{
int x;
float y;
bool b;
Vehicles car;
Aircraft helicopter;
cout << "The type of the variable x " << " is " << typeid(x).name() << endl;
cout << "The type of the variable y " << " is " << typeid(y).name() << endl;
cout << "The type of the variable b " << " is " << typeid(b).name() << endl;
cout << "The type of the object car " << " is " << typeid(car).name() << endl;
cout << "The type of the object helicopter " << " is " << typeid(helicopter).name() << endl;
}
上面的代码片段产生以下输出:
The type of the variable x is i
The type of the variable y is f
The type of the variable b is b
The type of the object car is 8Vehicles
The type of the object helicopter is 8Aircraft
如上所示,typeid.().name() 函数适用于简单变量和用户定义的类对象。 该函数分别返回 int、float 和 bool 的 i、f 和 b。
该函数返回对象汽车和直升机各自的类名称。 请务必注意,在此示例中,输出类名称前面有一个数字 8,因为类名称和变量名称在 C++ 上下文中的存储方式不同。
当与类目标一起使用时,typeid.().name() 函数依赖于系统和编译器,并且在返回类名时可以在类名之前附加任意字符。 在所使用的系统中,编译器会在类名之前附加类名中的字符数,即,Vehicle 和 Aircraft 的名称中都有 8 个字母。
由于 typeid.().name() 依赖于实现,因此它返回一个损坏的类名,该名称又可以使用 c++filt 命令或 __cxa_demangle 进行拆解。
尽管类名以损坏的形式返回,但它仍然可读并且可用于识别对象的类。 由于返回类型是字符串,因此可以轻松比较对象以检查它们是否具有相同的类。
使用 typeid.().name() 和 __cxa_demangle
查找 C++ 中类对象的类型
如前面的示例所示,使用 typeid.().name() 返回损坏的类名和变量类型。 为了获得分解结果,我们可以使用 cxxabi.h 库中的 __cxa_demangle 函数。
__cxa_demangle
的语法如下:
char *abi::__cxa_demangle(const char *mangled_name, char *output_buffer, size_t *length, int *status)
下面的示例显示了 __cxa_demangle
与 typeid.().name()
的使用:
#include<iostream>
#include<cxxabi.h>
using namespace std;
class Vehicles
{
public:
string make;
string model;
string year;
};
class Aircraft
{
public:
string make;
string model;
string year;
};
int main()
{
int x;
float y;
bool b;
Vehicles car;
Aircraft helicopter;
cout << "The type of the variable x " << " is " << abi::__cxa_demangle(typeid(x).name(), nullptr, nullptr, nullptr) << endl;
cout << "The type of the variable y " << " is " << abi::__cxa_demangle(typeid(y).name(), nullptr, nullptr, nullptr) << endl;
cout << "The type of the variable b " << " is " << abi::__cxa_demangle(typeid(b).name(), nullptr, nullptr, nullptr) << endl;
cout << "The type of the object car " << " is " << abi::__cxa_demangle(typeid(car).name(), nullptr, nullptr, nullptr) << endl;
cout << "The type of the object helicopter " << " is " << abi::__cxa_demangle(typeid(helicopter).name(), nullptr, nullptr, nullptr) << endl;
}
在此示例的范围内,我们只关心 __cxa_demangle
函数的第一个参数,即损坏的名称。 本案不需要其余的论点。
上面的代码产生以下输出:
The type of the variable x is int
The type of the variable y is float
The type of the variable b is bool
The type of the object car is Vehicles
The type of the object helicopter is Aircraft
从输出中可以看出,返回的变量和类型名称已被分解,并且与代码文件中的情况完全相同。
相关文章
Arduino 复位
发布时间:2024/03/13 浏览次数:315 分类:C++
-
可以通过使用复位按钮,Softwarereset 库和 Adafruit SleepyDog 库来复位 Arduino。
Arduino 的字符转换为整型
发布时间:2024/03/13 浏览次数:181 分类:C++
-
可以使用简单的方法 toInt()函数和 Serial.parseInt()函数将 char 转换为 int。
Arduino 串口打印多个变量
发布时间:2024/03/13 浏览次数:381 分类:C++
-
可以使用 Serial.print()和 Serial.println()函数在串口监视器上显示变量值。