欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

C++typeid關(guān)鍵字

typeid 是 C++ 的關(guān)鍵字之一,用于獲取運(yùn)行時(shí)類型信息,typeid操作符的返回結(jié)果是名為type_info的標(biāo)準(zhǔn)庫類型的對(duì)象的引用(在頭文件typeinfo中定義)。

成都地區(qū)優(yōu)秀IDC服務(wù)器托管提供商(創(chuàng)新互聯(lián)).為客戶提供專業(yè)的綿陽機(jī)房托管,四川各地服務(wù)器托管,綿陽機(jī)房托管、多線服務(wù)器托管.托管咨詢專線:18980820575

上測(cè)試代碼:

#include <assert.h>
#include <iostream>
#include <string>
#include <typeinfo>
#include <vector>
#include <cstdio>

using namespace std;

int func(int a) {
    return 0;
}

typedef int(*fun_ptr)(int);

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

int test_typeid1() {
    char              char_ = 'a';
    unsigned char     uchar_ = 'b';
    short             short_ = -16;
    unsigned short    ushort_ = 16;
    int               int_ = -1024;
    unsigned int      uint_ = 1024;
    float             float_ = 1.2f;
    double            double_ = 2.34;
    long              long_ = -12222;
    long long         llong_ = 12222;
    int array[10] = { 0 };
    int* array_ptr = array;
    string            string_("xiongwei");
    vector<int>       int_vector;
    fun_ptr  f = func;
    Base              base_;
    Base* pBase = new Base;
    Base& rBase = base_;

    cout << "char_ type: " << typeid(char_).name() << std::endl;
    assert(typeid(char).name() == typeid(char_).name());

    std::cout << "uchar type: " << typeid(uchar_).name() << std::endl; // uchar type: unsigned char
    assert(typeid(unsigned char).name() == typeid(uchar_).name());

    std::cout << "short_ type: " << typeid(short_).name() << std::endl; // short_ type: short
    assert(typeid(short).name() == typeid(short_).name());

    std::cout << "ushort_ type: " << typeid(ushort_).name() << std::endl; // ushort_ type: unsigned short
    assert(typeid(unsigned short).name() == typeid(ushort_).name());

    std::cout << "int_ type: " << typeid(int_).name() << std::endl; // int_ type: int
    assert(typeid(int).name() == typeid(int_).name());

    std::cout << "uint_ type: " << typeid(uint_).name() << std::endl; // uint_ type: unsigned int
    assert(typeid(unsigned int).name() == typeid(uint_).name());

    std::cout << "float_ type: " << typeid(float_).name() << std::endl; // float_ type: float
    assert(typeid(float).name() == typeid(float_).name());

    std::cout << "double_ type: " << typeid(double_).name() << std::endl; // double_ type: double
    assert(typeid(double).name() == typeid(double_).name());

    std::cout << "long_ type: " << typeid(long_).name() << std::endl; // long_ type: long
    assert(typeid(long).name() == typeid(long_).name());

    std::cout << "llong_ type: " << typeid(llong_).name() << std::endl; // llong_ type: __int64
    assert(typeid(long long).name() == typeid(llong_).name());

    std::cout << "array[] type: " << typeid(array).name() << std::endl; // array[] type: int [10]
    assert(typeid(int[10]).name() == typeid(array).name());

    std::cout << "array_header type: " << typeid(array_ptr).name() << std::endl; // array_header type: int * __ptr64
    assert(typeid(int*).name() == typeid(array_ptr).name());

    std::cout << "string_ type: " << typeid(string_).name() << std::endl; // string_ type: class std::basic_string<char,struct std::char_traits<char>, class std::allocator<char>>
    assert(typeid(std::string).name() == typeid(string_).name());

    std::cout << "int_vector type: " << typeid(int_vector).name() << std::endl; // int_vector type: class std::vector<int,class std::allocator<int>>
    assert(typeid(std::vector<int>).name() == typeid(int_vector).name());

    std::cout << "f type: " << typeid(f).name() << std::endl; // f type : int(__cdecl*)(int)
    assert(typeid(int(*)(int)).name() == typeid(f).name());

    std::cout << "Base_ type: " << typeid(base_).name() << std::endl; // Base_ type: class Base
    assert(typeid(class Base).name() == typeid(base_).name());

    std::cout << "pBase_ type: " << typeid(pBase).name() << std::endl; // pBase_ type: class Base * __ptr64
    assert(typeid(class Base*).name() == typeid(pBase).name());

    std::cout << "rBase_ type: " << typeid(rBase).name() << std::endl; // Base__ type: class Base
    assert(typeid(class Base&).name() == typeid(rBase).name());
    return 0;

}

void test_typeid2() {

    struct Base {

    }; // non-polymorphic
    struct Derived : Base {

    };

    struct Base2 {
        virtual void foo() {}
    }; // polymorphic

    struct Derived2 : Base2 {

    };

    int myint = 50;
    std::string mystr = "string";
    double *mydoubleptr = NULL;

    std::cout << "myint has type: " << typeid(myint).name() << '\n' // myint has type: int
        << "mystr has type: " << typeid(mystr).name() << '\n' // mystr has type: class std::basic_string<char, struct std::char_traits<char>, class std::allocator<char>>
        << "mydoubleptr has type: " << typeid(mydoubleptr).name() << '\n'; // mydoubleptr has type: double * __ptr64

                                                                           // std::cout << myint is a glvalue expression of polymorphic type; it is evaluated
    const std::type_info& r1 = typeid(std::cout); // 50

    std::cout << "#std::cout<<myint has type : " << r1.name() << '\n';
    // std::cout<<myint has type: class std::basic_ostream<char, struct std::char_traits<char>>

    const std::type_info& r2 = typeid(std::printf("%d\n", myint));
    std::cout << "printf(\"%d\\n\",myint) has type : " << r2.name() << '\n'; // printf(\"%d\\n\",myint) has type : int

    Derived d1;
    Base& b1 = d1;
    std::cout << "reference to non-polymorphic base: " << typeid(b1).name() << '\n'; // reference to non-polymorphic base: struct 'int __cdecl test_typeid2(void)'::'2'::Base

    Derived2 d2;
    Base2& b2 = d2;
    std::cout << "reference to polymorphic base: " << typeid(b2).name() << '\n'; // reference to polymorphic base: struct 'int __cdecl test_typeid2(void)'::'3'::Derived2

    try {
        // dereferencing a null pointer: okay for a non-polymoprhic expression  
        std::cout << "mydoubleptr points to " << typeid(*mydoubleptr).name() << '\n'; // mydoubleptr points to double  
                                                                                      // dereferencing a null pointer: not okay for a polymorphic lvalue  
        Derived2* bad_ptr = NULL;
        std::cout << "bad_ptr points to...";  // bad_ptr points to...   
        std::cout << typeid(*bad_ptr).name() << '\n';
    }
    catch (const std::bad_typeid& e) {
        std::cout << " caught " << e.what() << '\n'; // caught Attempted a typeid of NULL pointer!  
    }

}

template < typename T >
T max(T arg1, T arg2) {
    std::cout << typeid(T).name() << "s compared." << std::endl;
    return (arg1 > arg2 ? arg1 : arg2);
}

int test_typeid3()
{
    class Base {
    public:
        virtual void vvfunc() {}
    };

    class Derived : public Base {};

    Derived* pd = new Derived;
    Base* pb = pd;
    std::cout << typeid(pb).name() << std::endl;   //prints "class Base *" // class 'int __cdecl test_typeid3(void)'::'2'::Base * __ptr64  
    std::cout << typeid(*pb).name() << std::endl;   //prints "class Derived" // class 'int __cdecl test_typeid3(void)'::'2'::Derived  
    std::cout << typeid(pd).name() << std::endl;   //prints "class Derived *" // class 'int __cdecl test_typeid3(void)'::'2'::Derived * __ptr64  
    std::cout << typeid(*pd).name() << std::endl;   //prints "class Derived" // class 'int __cdecl test_typeid3(void)'::'2'::Derived  
    delete pd;

    float a = 1.2, b = 3.4;
    max(a, b); // floats compared  

    max<int>(1,2);

    return 0;
}

int main() {

    test_typeid1();
    cout << "---------------------------------------" << endl;
    test_typeid2();
    test_typeid3();

    system("pause");
    return 0;
}

新聞標(biāo)題:C++typeid關(guān)鍵字
網(wǎng)站URL:http://aaarwkj.com/article28/pjcgjp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、定制開發(fā)、企業(yè)網(wǎng)站制作App設(shè)計(jì)、微信小程序、響應(yīng)式網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營
成年人免费在线观看国产| 亚洲成av在线免费不卡| 国产精品国产一级国产av| 久久热久久热精品视频| 欧美丝袜熟女日韩亚洲| 在线激情av中文字幕| 亚洲国产欲色有一二欲色| 国产91在线精品超碰人人| 国产熟女真实乱精品视频| 国产美女主播在线精品一区| 一区二区三区日韩国产电影| 午夜精品视频免费91| 免费高清视频一区二区在线观看| 色橹橹欧美午夜精品福利| 看看美女阴逼毛茸茸的| 日韩性生活视频免费播放| 国产欧美一区二区三区高清| 麻豆精品情欲人妻一区| 国产精品免费观看在线国产| 亚洲一区二区三区欧美精品 | 香蕉视频在线观看亚洲精品| 熟女一区二区中文字幕| 精品一区二区三区亚洲| 97在线亚洲欧美视频| 亚洲日本精品国产第一区| 国产乱国产乱老熟部视频| 国产经典三级在线看| 91欧美精品一区二区| 国产精品一区二区熟女| 青青草青青草在线观看视频| 亚洲六月丁香六月婷婷| 香蕉欧美在线视频播放| 手机在线观看av大片| 综合久久久精品国产亚洲av| 欧美大片免费在线播放| 国产精品超碰在线观看| 国产成人免费高清av| 久久国产精品午夜视频| 亚洲精品伦理视频在线| 国产精品毛片在线看不卡| 最近中文字幕免费手机版|