• C++ typeid typename使用


    类型标识符(typeid

    标准C++的一个新特征是RTTIRun-Time Type Information运行时类型信息),它为程序在运行时确定对象类型,提供了一种标准方法。在标准C++中,有三个支持RTTI的元素:

    <!--[if !supportLists]-->1.         <!--[endif]-->关键字dynamic_cast(动态强制转换):操作符dynamic_cast将一个指向基类的指针转换为一个指向派生类的指针(如果不能正确转换,则返回0——空指针),格式为:

    dynamic_cast < type-id > ( exdivssion )

    dynamic_cast在转化过程中要用到相关类的类型信息类type_info中的信息。该关键字在前面的小小节1.2)中已经介绍过。

    <!--[if !supportLists]-->2.         <!--[endif]-->关键字typeid(类型标识符):用来检测指针类型(返回type_info类对象的指针),格式为:

    typeid ( exdivssion ) typeid ( type-id )

    其中,exdivssion为结果为对象的表达式,type-id为类名。

    <!--[if !supportLists]-->3.         <!--[endif]-->type_info(类型信息):存储特定类型的有关信息,定义在<typeinfo>头文件中。type_info类的具体内容由编译器实现来决定,但是至少必须包含返回字符串的name()成员函数。下面是type_info类的VC05实现版本:

    class type_info { // VC05中定义的简化

    public:

        virtual ~type_info();

        bool operator==(const type_info& rhs) const;

        bool operator!=(const type_info& rhs) const;

        int before(const type_info& rhs) const;

        const char* name() const;

        const char* raw_name() const;

    private:

        void *_m_data;

        char _m_d_name[1];

        type_info(const type_info& rhs);

        type_info& operator=(const type_info& rhs);

        static const char *_Name_base(const type_info *prhs, __type_info_node* __ptype_info_node);

        static void _Type_info_dtor(type_info *prhs);

    };

    例如:(可建立一个名为tmp的“Visual C++/常规/空项目”型项目,将如下两个文件加入到该项目中)

    // tmp.h

    template<class T> class A { };

     

    // tmp.cpp

    #include <typeinfo.h>

    #include <iostream>

    #include "tmp.h"

    using namespace std;

     

    int main( ){

           A<int> a;

           A<char> b;

           cout << typeid(a).name() << endl;

           cout << typeid(b).name() << endl;

           if (typeid(a) == typeid(b)) cout << "a==b" << endl;

           else cout << "a!=b" << endl << endl;

           cout << endl;

     

           int i;

           cout << typeid(int).name() << endl;

           cout << typeid(i).name() << endl;

           if (typeid(int) == typeid(i)) cout << "typeid(int) = typeid(i)" << endl;

           else cout << "typeid(int) != typeid(i)"<< endl;

           cout << endl;

    }

     

    输出结果为:

    class A<int>

    class A<char>

    a!=b

     

    int

    int

    typeid(int) = typeid(i)

     

    注意:只有对包含虚函数的抽象类层次,使用RTTI才有实际意义。

    8)类型名(typename

    对于有的嵌套类中的标识符,本来应该作为类型来处理,但是编译器并不知道这一点,而可能把它当成了静态变量。

    对模板中出现的一个标识符,若编译器既可以把它当作一个类型,又可以把它视为一个变量、对象、枚举、函数或模板时,则编译器一般不会认为这个标识符是类型,而认为它是一个其他元素(例如是变量或对象)。

    解决办法是,使用标准C++新增加的关键字typename,来明确告诉编译器,它后面的标识符是一个类型名,而不是其他什么东西。

    例如:

    template<class T> class X {

           typename T::id i; // 如果没有typename来说明,编译器会将T::id当成静态变量

    public:

           void f ( ) { i.g( ); }

    };

    class Y {

    public:

           class id {

           public:

                  void g( ) { }

           };

    };

    int main ( ) {

           X<Y> xy;

           xy.f ( );

    }

    最后一种用法是说,可以用typename来代替模板声明中的类型参数class,即:可将

    template<class T> ……

    改为

    template<typename T> ……

    而且这样更名符其实。因为除了类类型外,基本数据类型和结构等类型,也是可以作为模板的类型参数的。

    例如:(能够打印任意标准C++序列容器中的数据的函数模板)

    // PrintSeq.cpp

    #include 
    <iostream>

    #include <list>

    #include <memory>

    #include <vector>


    using namespace std;

     

    template
    <class T, template<class U, class = allocator<U> > class Seq>

    void printSeq(Seq<T>& seq) {

           
    for (typename Seq<T>::iterator b = seq.begin(); b != seq.end(); b++)

                  cout 
    << *<< endl;

    }


     

    int main ( ) {

           
    // 处理矢量

           vector
    <int> v;

           v.push_back(
    1);  v.push_back(2);

           printSeq(v);

           
    // 处理表

           list
    <int> lst;

           lst.push_back(
    3);         lst.push_back(4);

           printSeq(lst);

    }


    输出为:

    1

    2

    3

    4

    注意:关键字typename并不能创建一个新类型名,它只是通知编译器,将标识符解释为类型。若想创建一个新类型名,你可以使用关键字typedef。例如

    typename Seq<T>::iterator It; // 告诉编译器iterator是类型,It是该类型的变量

    typedef typename Seq<T>::iterator It; // 创建了一个与iterator等价的新类型名It

  • 相关阅读:
    捋一下Redis
    docker 的简单操作
    opencv的级联分类器(mac)
    python日常
    pip安装显示 is not a supported wheel on this platform.
    字节流与字符流的区别详解
    请求转发和重定向的区别及应用场景分析
    Eclipse的快捷键使用总结
    Eclipse给方法或者类添加自动注释
    IntelliJ IDEA 连接数据库 详细过程
  • 原文地址:https://www.cnblogs.com/gaojun/p/1688458.html
Copyright © 2020-2023  润新知