gpt4 book ai didi

c++ - 使不可变类型可变(consts 和 boost)

转载 作者:行者123 更新时间:2023-11-28 07:39:15 25 4
gpt4 key购买 nike

我正在为我无法更改的代码库开发一项功能(除了我正在编写的内容),这里有一些类型:

// 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);

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

最佳答案

MPtr foo = boost::const_pointer_cast<Thingy>(moo);

关于c++ - 使不可变类型可变(consts 和 boost),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16158844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com