Using c++ typedef/using type alias

后端 未结 3 864
不知归路
不知归路 2021-01-03 21:59

I am reading C++ primer book and completely don\'t understand one line:

 using int_array = int[4]; 
 typedef int int_array[4]; // This line
 for (int_array *         


        
3条回答
  •  盖世英雄少女心
    2021-01-03 22:13

    Both type aliases are the same:


    Type alias, alias template (since C++11):
    Type alias is a name that refers to a previously defined type (similar to typedef):

    using identifier attr(optional) = type-id ; 
    

    so you may use:

    typedef int int_array[4];
    

    or you may just use (it is the same as above):

    using int_array = int[4];
    

    When you need to address the memory with 4*sizeof(int) steps, e.g. if the system int size is 4 bytes, then the memory step size is 4*4=16 bytes. even you may use int_array *p; in this case ++p advances p by one memory step e.g. 16 bytes. see:


    1- working sample with using int_array = int[4];:

    #include 
    using std::cout; using std::endl;
    
    int main()
    {
        int ia[3][4] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
    
        // a range for to manage the iteration
        // use type alias
        using int_array = int[4];
        for (int_array& p : ia)
            for (int q : p)
                cout << q << " ";
        cout << endl;
    
        // ordinary for loop using subscripts
        for (size_t i = 0; i != 3; ++i)
            for (size_t j = 0; j != 4; ++j)
                cout << ia[i][j] << " ";
        cout << endl;
    
        // using pointers.
        // use type alias
        for (int_array* p = ia; p != ia + 3; ++p)
            for (int *q = *p; q != *p + 4; ++q)
                cout << *q << " ";
        cout << endl;
    
        return 0;
    }
    

    output 1:

    0 1 2 3 4 5 6 7 8 9 10 11
    0 1 2 3 4 5 6 7 8 9 10 11
    0 1 2 3 4 5 6 7 8 9 10 11
    

    2- working sample using typedef int int_array[4];:

    #include 
    using std::cout; using std::endl;
    
    int main()
    {
        int ia[3][4] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
    
        // a range for to manage the iteration
        // use type alias
        typedef int int_array[4];
        for (int_array& p : ia)
            for (int q : p)
                cout << q << " ";
        cout << endl;
    
        // ordinary for loop using subscripts
        for (size_t i = 0; i != 3; ++i)
            for (size_t j = 0; j != 4; ++j)
                cout << ia[i][j] << " ";
        cout << endl;
    
        // using pointers.
        // use type alias
        for (int_array* p = ia; p != ia + 3; ++p)
            for (int *q = *p; q != *p + 4; ++q)
                cout << *q << " ";
        cout << endl;
    
        return 0;
    }
    

    output 2(same):

    0 1 2 3 4 5 6 7 8 9 10 11
    0 1 2 3 4 5 6 7 8 9 10 11
    0 1 2 3 4 5 6 7 8 9 10 11
    

    ref: https://github.com/Mooophy/Cpp-Primer/blob/master/ch03/ex3_44.cpp
    note: use -std=c++11 for compile/link.


    3- just for demo(excerpt):
    some real world usage in embedded system:

    extern const char kpd2ascii[6][6] PROGMEM;
    typedef const char cint8a6_t[6];
    cint8a6_t *p;
    p = kpd2ascii;
    kpdBuffer = pgm_read_byte(&p[row][col - 1]);
    

    I hope this helps.

提交回复
热议问题