溯造型的shared_ptr&LT;基地以及GT;对于shared_ptr&LT;衍生GT ;? [英] Downcasting shared_ptr<Base> to shared_ptr<Derived>?
问题描述
更新:在这个例子中,shared_ptr的是像在加速,但它不支持shared_polymorphic_downcast(或dynamic_pointer_cast或static_pointer_cast为此事)
Update: the shared_ptr in this example is like the one in Boost, but it doesn't support shared_polymorphic_downcast (or dynamic_pointer_cast or static_pointer_cast for that matter)!
我想初始化一个共享指针派生类中不失引用计数:
I'm trying to initialize a shared pointer to a derived class without losing the reference count:
struct Base { };
struct Derived : public Base { };
shared_ptr<Base> base(new Base());
shared_ptr<Derived> derived;
// error: invalid conversion from 'Base* const' to 'Derived*'
derived = base;
到目前为止,一切都很好。没想到C ++库*隐式转换为派生*。不过,我想用code pssed功能前$ P $(即保持引用计数,而向下转换的基指针)。我首先想到的是提供基本转换运算符,使派生可能发生的隐式转换(学究:我会检查下投是有效的,不用担心):
So far, so good. I didn't expect C++ to implicitly convert Base* to Derived*. However, I do want the functionality expressed by the code (that is, maintaining the reference count while downcasting the base pointer). My first thought was to provide a cast operator in Base so that an implicit conversion to Derived could take place (for pedants: I would check that the down cast is valid, don't worry):
struct Base {
operator Derived* ();
}
// ...
Base::operator Derived* () {
return down_cast<Derived*>(this);
}
好吧,这并没有帮助。这似乎编译器完全忽略了我的类型转换操作符。任何想法我怎么可能让shared_ptr的分配工作?对于加分:什么样类型的基础* const的
是? 常量基地*
我理解,但基础* const的
?是什么常量
是指在这种情况下?
Well, it didn't help. It seems the compiler completely ignored my typecast operator. Any ideas how I could make the shared_ptr assignment work? For extra points: what kind of type Base* const
is? const Base*
I understand, but Base* const
? What does const
refer to in this case?
推荐答案
我假设你正在使用的boost :: shared_ptr的
...我想你想的 dynamic_pointer_cast
一个>或 shared_polymorphic_downcast
。
I assume you're using boost::shared_ptr
... I think you want dynamic_pointer_cast
or shared_polymorphic_downcast
.
这些都需要多态类型,但是。
These require polymorphic types, however.
什么样类型的基础* const的
是? 常量基地*
我理解,但基础* const的
?是什么常量
是指在这种情况下?
what kind of type
Base* const
is?const Base*
I understand, butBase* const
? What doesconst
refer to in this case?
-
常量基地*
是一个可变指向一个恒定的基本
。 -
基础常量*
是一个可变指向一个恒定的基本
。 -
基础* const的
是一个常量指针可变基本
。 -
基础常量* const的
是一个常量指针,以一个恒定的基本
。
const Base *
is a mutable pointer to a constantBase
.Base const *
is a mutable pointer to a constantBase
.Base * const
is a constant pointer to a mutableBase
.Base const * const
is a constant pointer to a constantBase
.
下面是一个小例子:
struct Base { virtual ~Base() { } }; // dynamic casts require polymorphic types
struct Derived : public Base { };
boost::shared_ptr<Base> base(new Base());
boost::shared_ptr<Derived> derived;
derived = boost::static_pointer_cast<Derived>(base);
derived = boost::dynamic_pointer_cast<Derived>(base);
derived = boost::shared_polymorphic_downcast<Derived>(base);
我不知道这是否是故意的,你的命令创建类型的实例,并施放它,但它起到很好的说明了差异。
I'm not sure if it was intentional that your example creates an instance of the base type and casts it, but it serves to illustrate the difference nicely.
的 static_pointer_cast
将只管去做。这将导致不确定的行为(A 派生*
在内存中分配并初始化指向由基本
),并很可能引起崩溃,甚至更糟。在基础引用计数
将递增。
The static_pointer_cast
will "just do it". This will result in undefined behavior (a Derived*
pointing at memory allocated for and initialized by Base
) and will likely cause a crash, or worse. The reference count on base
will be incremented.
的 dynamic_pointer_cast
将导致空指针。在基
引用计数将保持不变。
The dynamic_pointer_cast
will result in a null pointer. The reference count on base
will be unchanged.
的 shared_polymorphic_downcast
将有同样的结果作为一个静态的演员,但会触发一个断言,而似乎获得成功,并导致未定义的行为。在基础引用计数
将递增。
The shared_polymorphic_downcast
will have the same result as a static cast, but will trigger an assertion, rather seeming to succeed and leading to undefined behavior. The reference count on base
will be incremented.
请参阅(死链接):
有时是有点难以决定是否使用的static_cast
或的dynamic_cast
,你希望你能有两个世界的一点点。它公知的是将dynamic_cast具有运行时开销,但它是安全的,而的static_cast具有所有没有开销,但它将会失效。这将是多么美好,如果你能在调试使用 shared_dynamic_cast
构建和 shared_static_cast
在发布版本。那么,这样的事情已经是可以的,叫做 shared_polymorphic_downcast
。
Sometimes it is a little hard to decide whether to use
static_cast
ordynamic_cast
, and you wish you could have a little bit of both worlds. It is well known that dynamic_cast has a runtime overhead, but it is safer, whereas static_cast has no overhead at all, but it may fail silently. How nice it would be if you could useshared_dynamic_cast
in debug builds, andshared_static_cast
in release builds. Well, such a thing is already available and is calledshared_polymorphic_downcast
.
这篇关于溯造型的shared_ptr&LT;基地以及GT;对于shared_ptr&LT;衍生GT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!