2

I am getting this compile error:

error: invalid conversion from 'const MyClass*' to 'MyClass*'

Here is the code:

std::tr1::shared_ptr<MyClass> myClassA;
const MyClass* myClassB;
myClassA = std::tr1::shared_ptr<MyClass>(myClassB); // error here

I think I understand the error, just don't know how to fix. I need myClassB to be a const so how to convert/copy classB to a shared_ptr?

kanso
  • 750
  • 8
  • 17

2 Answers2

8

You'll need a shared pointer to a const object:

std::tr1::shared_ptr<const MyClass> myClassA;
                     ^^^^^
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
0

You can't go from a const MyClass to MyClass.

myClassA = std::tr1::shared_ptr< **const** MyClass>(myClassB);
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
Caesar
  • 9,483
  • 8
  • 40
  • 66
  • 1
    In a language where `*` has meaning it is awkward to use it for emphasis. You could use Clang's way of indicating bits by underlining with `^~~~~` for example. – Matthieu M. Jul 14 '12 at 16:08
  • @Matthieu M I know that, I wanted that part to be bold but you can't bold something when it is in a code. So I didn't put it in code but my editor decided to put it in code. – Caesar Jul 14 '12 at 16:17