Tell me how to fix the following error:

There is a data structure:

struct CMyData { int m_data; }; 

There is a constant pointer to this structure.

 const CMyData* ptr = _get(); 

I want to write to another structure a pointer to the data from the first

 myData2.m_ptr = &ptr->m_data; 

And I get an error

error C2440: '=': cannot convert from 'const CMyData :: int *' to 'CMyData :: int *'

Corrected by abolishing the constant, i.e. So:

 CMyData* ptr = _get(); 

But I want to adhere to the minimum functionality - I'm not going to change the ptr itself, that's why I declared it a constant.

  • 3
    Well, in the second structure, declare the m_ptr field not as int* , but as const int * ... - Harry
  • four
    const CMyData* ptr is not a "constant pointer", but a "pointer to a constant structure" - Fat-Zer
  • Harry, to be honest, never met such will there be a constant member in the structure and at the same time I can only change it from the constructor during initialization and in the situation I described above? PS I understood my mistake, Harry thanks - Zhihar
  • @ Fat-Zer this is the reason for my misunderstanding :) Now I understand, really const belonged to another part. - Zhihar 2:53
  • @Zhihar, can you write the answer yourself? - Fat-Zer

0