使不可变类型可变(consts和boost)

时间:2013-04-23 00:13:57

标签: c++ boost mutable

我正在研究一个我无法改变的代码库功能(除了我正在编写的内容),这里有一些类型:

// Pointer to a mutable thingy
typedef boost::shared_ptr<Thingy>       MPtr;

// Pointer to an immutable thingy
typedef boost::shared_ptr<const Thingy> Ptr;

现在,我有一个MPtr类型的对象,我需要分配给Ptr类型的对象,但我不能(编译器告诉我没有可能的转换)。使用const_cast似乎也没有帮助:

MPtr foo = const_cast<MPtr*>(moo);

我收到一条错误消息,说它无法更改基础类型。关于如何解决这个问题的任何想法?

1 个答案:

答案 0 :(得分:1)

MPtr foo = boost::const_pointer_cast<Thingy>(moo);
相关问题