Const TypedeffedIntPointer is not equal to const int *

I have the following C ++ code:

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

I compile this using Visual Studio 2008 and get the following error:

error C2440: 'initializing': cannot convert from 'const int *' to 'const IntPtr'

Clearly, my understanding of typedefs is not what it should be.

The reason I ask, I save the pointer type on the STL map. I have a function that returns a const pointer that I would like to use to search on a map (using map :: find (const key_type &). Since

const MyType* 

and

const map<MyType*, somedata>::key_type

incompatible, I have problems.

Relations Dirk

+3
source share
2 answers

const IntPtr ctip4, const-pointer-to-int, const int * cip --const-int. , .

/ cip

int * const cip = new int;

, const MyType * ( , , , , MyType ), const_casting , :

#include <map>

int main()
{
    const int * cpi = some_func();

    std::map<const int *, int> const_int_ptr_map;
    const_int_ptr_map.find(cpi); //ok

    std::map<int *, int> int_ptr_map;
    int_ptr_map.find(const_cast<int *>(cpi)); //ok
}
+7

const IntPtr int* const, const int*.

, const int, const int.

, typedefs:

typedef int* IntPtr;
typedef const int* ConstIntPtr;

ConstIntPtr, const int.

+5

Source: https://habr.com/ru/post/1744355/


All Articles