I have the following C++ code:
typedef int* IntPtr;
const int* cip = new int;
const IntPtr ctip4 = cip;
I compile this with 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 is should be.
The reason I’m asking, I’m storing a pointer type in a STL map. I have a function that returns a const pointer which I would like to use to search in the map (using map::find(const key_type&). Since
const MyType*
and
const map<MyType*, somedata>::key_type
is incompatible, I’m having problems.
Regards
Dirk
When you write
const IntPtr ctip4, you are declaring a const-pointer-to-int, whereasconst int * cipdeclares a pointer-to-const-int. These are not the same, hence the conversion is impossible.You need to change the declaration/initialization of cip to
To resolve this issue in your example, you need to either change the key type of the map to
const MyType *(whether or not it makes sense depends on your application, but I think that changing a MyType object via a pointer used as key in the map is unlikely), or fall back to const_casting the parameter to find: