const TypedeffedIntPointer not equal to const int *

后端 未结 2 498
误落风尘
误落风尘 2021-01-20 21:47

I have the following C++ code:

typedef int* IntPtr;
const int* cip = new int;
const IntPtr ctip4 = cip;

I compile this with Visual Studio 2

2条回答
  •  既然无缘
    2021-01-20 21:50

    const IntPtr is the same as int* const, not const int*.

    That is, it is a const pointer to an int, not a pointer to a const int.

    A solution would be to provide two typedefs:

    typedef int* IntPtr;
    typedef const int* ConstIntPtr;
    

    and use ConstIntPtr when you need a pointer to a const int.

提交回复
热议问题