Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 767253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T17:10:56+00:00 2026-05-14T17:10:56+00:00

I have the following C++ code: typedef int* IntPtr; const int* cip = new

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T17:10:57+00:00Added an answer on May 14, 2026 at 5:10 pm

    When you write const IntPtr ctip4, you are declaring a const-pointer-to-int, whereas const int * cip declares 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

    int * const cip = new int;
    

    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:

    #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
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code which compiles and runs fine under Visual Studio 2008
I have the following code: #include<iostream> using namespace std; typedef void (*HandlerFunc)(int, int); HandlerFunc
I have the following code: template <typename Provider> inline void use() { typedef Provider::Data<int>
I have following code: typedef int (*t_Function) (int x); t_Function Functions[MAX_FUNCTIONS]; int f(int x)
I have the following code: typedef unsigned char some_type[6]; int main() { some_type some_var1;
I have the following typedef's in my code: typedef unsigned long int ulint; typedef
I have the following piece of code: typedef int AliasB; typedef unsigned short AliasA;
I have the following code: typedef vector<int> Vec; typedef vector<Vec> VecOfVec; template<typename Vec> Vec
I have the following code: typedef void VOID; int f(void); int g(VOID); which compiles
Suppose I have the following code: typedef struct { char **p; } STRUCT; int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.