迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > C++ >

在 C++ 中查找对象类型

作者:迹忆客 最近更新:2023/08/19 浏览次数:

本文讨论在 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_demangletypeid.().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

从输出中可以看出,返回的变量和类型名称已被分解,并且与代码文件中的情况完全相同。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Arduino 中停止循环

发布时间:2024/03/13 浏览次数:444 分类:C++

可以使用 exit(0),无限循环和 Sleep_n0m1 库在 Arduino 中停止循环。

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()函数在串口监视器上显示变量值。

Arduino if 语句

发布时间:2024/03/13 浏览次数:123 分类:C++

可以使用 if 语句检查 Arduino 中的不同条件。

Arduino ICSP

发布时间:2024/03/13 浏览次数:214 分类:C++

ICSP 引脚用于两个 Arduino 之间的通信以及对 Arduino 引导加载程序进行编程。

使用 C++ 编程 Arduino

发布时间:2024/03/13 浏览次数:127 分类:C++

本教程将讨论使用 Arduino IDE 在 C++ 中对 Arduino 进行编程。

Arduino 中的子程序

发布时间:2024/03/13 浏览次数:168 分类:C++

可以通过在 Arduino 中声明函数来处理子程序。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便