invalid conversion from ‘const type*’ to ‘type*’

13,187

Solution 1

If you want to change the thing that ptr points to, then your function needs to take its argument by non-const reference (since MyClass2 might modify it):

MyClass2(MyClass1& obj) { ptr = &obj; }

Or, if you don't intend to modify the thing that ptr points to, then ptr should be declared as a pointer-to-const:

const MyClass1* ptr;

Both of these solutions will cause the code to compile.

Solution 2

Answering based on the last few comments.

I'll give you an example of const applied to pointers with ints,

int a = 2;
int b = 3;

int*              p1 = &a; // Modifiable pointer  modifiable value
const int*        p2 = &a; // Modifiable pointer  const value
int* const        p3 = &a; // Const pointer       modifiable value
const int * const p4 = &a; // Const pointer       const value

*p1 = 3; // Ok, modifiable left-value
*p2 = 4; // Error: non-modifiable left-value
*p3 = 5; // Ok
*p4 = 6; // Error

p1 = &b; // Ok: modifiable pointer
p2 = &b; // Ok
p3 = &b; // Error
p4 = &b; // Error

In your case, you're looking for a modifiable pointer but a const value. So you want the second case,

const MyClass1* ptr;

(Which is what you originally had)

It would seem you didn't actually try to change the pointer?

Solution 3

I known from the comment that you want to modify the content of pointer, not the object pointer points to, so please declare the ptr pointer as const MyClass1* ptr. This means that ptr is a pointer which refers to a const MyClass1. So you can change the pointer, but you are not able to modify the object referenced by the pointer.

Share:
13,187
gvgramazio
Author by

gvgramazio

Updated on June 29, 2022

Comments

  • gvgramazio
    gvgramazio almost 2 years

    I want to store through the constructor of a class a pointer to an object of another class. What is the correct way to do that?

    If I substitute MyClass1* ptr with const MyClass1* ptr there is no error, but in this case I think that i cannot change ptr anymore. What is the correct way to achieve what i want?

    example.cpp

    class MyClass1{
        public:
            int a;
            int b;
    };
    
    class MyClass2{
        MyClass1* ptr;
        public:
            MyClass2(const MyClass1& obj){ptr = &obj;};
    };
    
    int main(int argc, char* argv[]){
        MyClass1 object1;
        object1.a = object1.b = 0;
        MyClass2 object2(object1);
        
        return 0;
    }
    

    Compile it through g++ -o example example.cpp give me this error

    example.cpp: In constructor ‘MyClass2::MyClass2(const MyClass1&)’:

    example.cpp:10:37: error: invalid conversion from ‘const MyClass1*’ to ‘MyClass1*’ [-fpermissive]

    MyClass2(const MyClass1& obj){ptr = &obj;};

  • RamblingMad
    RamblingMad over 8 years
    const MyClass1& is unsafe because a temporary could be passed in
  • emlai
    emlai over 8 years
    It is also unsafe because a non-temporary object with a lifetime shorter than that of the Class2 object might be passed in. In general, storing non-owning references/pointers long-time is asking for trouble, because of these lifetime issues.
  • gvgramazio
    gvgramazio over 8 years
    @zenith: in my algorithm the object of Class1 has a lifetime longer that the one of the object of Class2 that have a pointer to the first one. However this is obviously not true in general. How can I take into account this occurrence?
  • RamblingMad
    RamblingMad over 8 years
    @giusva you can't. But you could use a shared_ptr to share ownership and keep an object alive.
  • emlai
    emlai over 8 years
    In C++, there's no mechanism in the compiler that would check whether you're managing the lifetimes of your referenced objects correctly (for example in Rust there is). You're on your own in that aspect. Although you can always choose to hack around it by sharing the ownership of the referenced object to ptr by using shared_ptr as mentioned by CoffeeandCode, which forces the referenced object to stay alive as long as some shared_ptr is pointing to it.