Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用typeid时获取不正确的类名-_C++_Typeid - Fatal编程技术网

C++ 使用typeid时获取不正确的类名-

C++ 使用typeid时获取不正确的类名-,c++,typeid,C++,Typeid,为了我自己的理解,我实施了以下计划。但是我看到typeid返回的类名被稍微修改了。我知道这可能是因为名字弄乱了,但包括外部C也没用 有人能帮我理解为什么会有这种行为,以及如何解决它吗 g++版本-4.7.0 #include <iostream> #include <typeinfo> using namespace std; class Base { public: virtual ~Base(){} }; class Derive : p

为了我自己的理解,我实施了以下计划。但是我看到typeid返回的类名被稍微修改了。我知道这可能是因为名字弄乱了,但包括外部C也没用

有人能帮我理解为什么会有这种行为,以及如何解决它吗

g++版本-4.7.0

#include <iostream>
#include <typeinfo>

using namespace std;

class Base
{
    public:
        virtual ~Base(){}
};

class Derive : public Base
{
    public:
        ~Derive(){}
};

class newBase
{
    public:
        ~newBase(){}
};

class newDerive : public newBase
{
    public:
        ~newDerive(){}
};

int main()
{
    Base base;
    Derive derive;
    Base *pBase;
    Base & rBase1 = base;
    Base & rBase2 = derive;

    newBase newbase;
    newDerive newderive;
    newBase *pNewBase;

    //Results with polymorphic class.
    pBase = &base;
    cout<<"Base class pointer pBase contains object of type "<<typeid(*pBase).name()            <<".\n";

    pBase = &derive;
    cout<<"Base class pointer pBase contains object of type "<<typeid(*pBase).name()<<".\n";

    cout<<"\nReference variable rBase1 referring to "<<typeid(rBase1).name()<<".\n";
    cout<<"Reference variable rBase2 referring to "<<typeid(rBase2).name()<<".\n";

    //Results with non-polymorphic class.
    pNewBase = &newbase;
    cout<<"\nBase class pointer pNewBase contains object of type "<<typeid(*pNewBase).name()<<".\n";

    pNewBase = &newderive;
    cout<<"Base class pointer pNewBase contains object of type "<<typeid(*pNewBase).name()<<".\n";

    return 0;
}

Output -
Base class pointer pBase contains object of type 4Base.
Base class pointer pBase contains object of type 6Derive.

Reference variable rBase1 referring to 4Base.
Reference variable rBase2 referring to 6Derive.

Base class pointer pNewBase contains object of type 7newBase.
Base class pointer pNewBase contains object of type 7newBase.
#包括
#包括
使用名称空间std;
阶级基础
{
公众:
虚拟~Base(){}
};
类派生:公共基
{
公众:
~derivate(){}
};
新基类
{
公众:
~newBase(){}
};
类newDerive:公共newBase
{
公众:
~newDerive(){}
};
int main()
{
基地;
派生;
碱基*pBase;
Base&rBase1=Base;
Base&rBase2=派生;
新基新基;
新派生新派生;
newBase*pNewBase;
//结果为多态类。
pBase=&base;

cout对于
type_info::name()
的外观没有要求

typeid
表达式的结果是静态类型
const std::type_info
(18.7.1)和动态类型的左值
const std::type_info
const name
,其中name是公开派生自
std::type_info

然后,关于std::type_info::name()

const char*name()const;

返回:实现定义的NTB

[……]

NTBS只是以null结尾的字节字符串的简写

换句话说:您不应该依赖于
type_info::name()
的任何值

您在g++中实际看到的内容: 这些名称是,而g++对这种混乱名称的实现是基于,其中每个子字符串是名称空间名称,加上一些其他信息;但基本上就是这样

例如:

unmangled: foo::bar::Frob
mangled:   3foo3bar4Frob
要放入编译器的示例:

#include <iostream>
#include <typeinfo>

namespace foo { namespace bar { 
    enum Frob {};
    class Frobnicate {};
    Frob frob;

    template <typename T> void Meh() { throw T(); }
} }

int main () {
    std::cout << typeid(foo::bar::Frob).name() << '\n'
              << typeid(foo::bar::Frobnicate).name()  << '\n'  
              << typeid(foo::bar::frob).name() << '\n'
              << typeid(foo::bar::Meh<int>).name() << '\n'
              << typeid(foo::bar::Meh<float>).name() << '\n'
    ;
}

后两种方法向您展示了一个人甚至不能依赖不同的名称。

如果您对“Demanling”名称感兴趣,g++有一个编译器特定的函数来实现这一点


你可以在

中找到一个例子,你可以提供一个你收到的名字和你期望的名字的例子,使你的问题更加完整。它仍然是重复的,这可能是dv的原因。@user93353-我运行了命令“/a.out | c++filt”但是它也没用。我还缺少什么吗?谢谢。船长同意,我应该给出输出。编辑了这篇文章。谢谢Phresnel。作为一个新手,我只是认为它应该返回程序中指定的类型名称。但是我想我现在很清楚了。你的解释很有帮助。再次感谢。“后两者向您表明,一个人甚至不能依赖名称的不同。”-但它们是不同的。@ChristianRau:facepalm(编辑:示例精炼)我认为这应该是一个评论。@Emilio:谢谢链接。我已经修改了我的程序,以避免名称混乱。
N3foo3bar4FrobE
N3foo3bar10FrobnicateE
N3foo3bar4FrobE
FvvE
FvvE