2

This is my code:

int i = 5;
const int * cpi = &i; //pointer to const
int * pi = cpi; //Error: a value of type "const int*" cannot be assigned to an entity of type "int*"
Clay
  • 4,700
  • 3
  • 33
  • 49
Sagar
  • 23
  • 1
  • 6

1 Answers1

0

i is mutable, cause it is not a const type. You try to store a int address in a const int address via cpi.

To solve it you have to actually hand over the values not the addresses.

3vilc00kie
  • 16
  • 2
  • Please suggest me any solution for error in the below piece of code. class B{ public: B(){} }; class A{ B *b; public: A(const B &bb) : b(&bb) {} //Getting Similar error here as well }; – Sagar Dec 31 '15 at 08:16
  • I think this for the same reason as your original question. C++ sees that the original variable `b` is not a const. You have to match the types in order to make this code running. I think the solution would be to either remove the `const` in the constructor of `A` or defining `b` as a const as well. I am not familiar with c++, maybe there is a casting convention where you can cast a const pointer to an mutable one – 3vilc00kie Dec 31 '15 at 09:03