How to create a strong typedef in C++(11) without an external library?

后端 未结 2 1447
轻奢々
轻奢々 2021-01-23 14:37

So, I know you can alias types with the following:
typedef int *intPtr
But the C++ compiler can\'t differentiate between them:

typedef int         


        
2条回答
  •  失恋的感觉
    2021-01-23 15:14

    A typedef or using statement will not introduce a new type.

    To get a new type you need to define one:

    struct foo { int x; };
    struct bar { int x; };
    
    int main()
    {
        //typedef int foo;
        //typedef int bar;
        foo x{5};
        bar y{7};
        int z = x + y;  // now doesn't compile, wants an operator+() defined  
        return 0;
    }
    

    In the above example we take advantage of aggregate initialization to allow for the use of the structs in this way.

提交回复
热议问题