[英]Polymorphism and shared_ptr passed by reference
I try to send to function a shared_ptr with polymorphic class. 我尝试发送具有多态类的shared_ptr函数。 My objective is to find a best way to send my shared_ptr without increase ref_count.
我的目标是找到一种无需增加ref_count即可发送我的shared_ptr的最佳方法。
EDIT: I don't search solution where my shared_ptr is replaced because I want to call shared_ptr.reset() for example. 编辑:我不搜索替换我的shared_ptr的解决方案,因为例如我想调用shared_ptr.reset()。
Currently, void doGenericTemplate(std::shared_ptr<CLASS>& ptr)
is what I want in result BUT I prefer a single function in program. 目前,
void doGenericTemplate(std::shared_ptr<CLASS>& ptr)
是我想要的结果,但是我更喜欢程序中的单个函数。
Moreover, I don't understand why the function void doGeneric(std::shared_ptr<Base>& ptr)
doesn't compile (equivalent without shared_ptr work fine: please check doClassic
in complete code). 而且,我不明白为什么
void doGeneric(std::shared_ptr<Base>& ptr)
函数无法编译(等效于不使用shared_ptr的情况也很好:请检查完整代码中的doClassic
)。
Thanks you ! 谢谢 !
#include <iostream>
#include <memory>
class Base
{
public:
Base() = default;
virtual ~Base() = default;
virtual void run() = 0;
};
class Derived1: public Base
{
public:
Derived1() = default;
virtual ~Derived1() = default;
void run()
{
std::cout << " Derived1";
}
};
class Derived2: public Base
{
public:
Derived2() = default;
virtual ~Derived2() = default;
void run()
{
std::cout << " Derived2";
}
};
// This function works but increase count
void doGenericCopy(std::shared_ptr<Base> ptr)
{
ptr->run();
std::cout << " Ref count: " << ptr.use_count() << std::endl;
}
// This function works without increase count = OK !
void doSpecificD1(std::shared_ptr<Derived1>& ptr)
{
ptr->run();
std::cout << " Ref count: " << ptr.use_count() << std::endl;
}
// Compilation error = FAILED !
void doGeneric(std::shared_ptr<Base>& ptr)
{
ptr->run();
std::cout << " Ref count: " << ptr.use_count() << std::endl;
}
// Working fine for all Derivate = OK !
template<typename CLASS>
void doGenericTemplate(std::shared_ptr<CLASS>& ptr)
{
ptr->run();
std::cout << " Ref count: " << ptr.use_count() << std::endl;
}
int main()
{
auto d1 = std::make_shared<Derived1>();
auto d2 = std::make_shared<Derived2>();
std::cout << "With copy: " << std::endl;
doGenericCopy(d1);
doGenericCopy(d2);
std::cout << "Specific: " << std::endl;
doSpecificD1(d1);
std::cout << "Template: " << std::endl;
doGenericTemplate(d1);
doGenericTemplate(d2);
// Compilation issue
//doGeneric(d1);
}
https://ideone.com/ZL0v7z https://ideone.com/ZL0v7z
Currently in c++, shared_ptr has not in language a specific tools to use polymorphism of class inside template. 当前在c ++中,shared_ptr尚无特定语言的工具来使用模板内类的多态性。
The best way is to refactor my code and avoids to manage shared_ptr (ref_count, reset). 最好的方法是重构我的代码,并避免管理shared_ptr(ref_count,重置)。
Thanks guys ! 多谢你们 !
- Do you have another solution ?
您还有其他解决方案吗?
Pass object by reference or const reference instead of shared_ptr
. 通过引用或const引用而不是
shared_ptr
传递对象。
void doGeneric(Base& r)
{
r.run();
}
Firstly - this shows explicitly that you do not store or cache pointer somwhere. 首先-这明确表明您没有在任何地方存储或缓存指针。 Secondly - you avoid ambiguities like the one you presented here.
其次,您要避免像此处介绍的那样含糊不清。
- Do you have an explain ?
你有解释吗?
Passing shared_ptr<Derived>
to function causes implicit cast to shared_ptr<Base>
. 将
shared_ptr<Derived>
传递给函数会导致隐式强制转换为shared_ptr<Base>
。 This new shared_ptr<Base>
is temporary, so it can not be cast to shared_ptr<Base> &
. 此新的
shared_ptr<Base>
是临时的,因此无法将其shared_ptr<Base> &
为shared_ptr<Base> &
。 This implicit cast would increase ref count even if you could pass it. 即使您可以通过此隐式转换,也会增加引用计数。
A shared_ptr<Base>
and shared_ptr<Derived>
are unrelated types, except you can implicitly create a shared_ptr<Base>
from a shared_ptr<Derived>
. shared_ptr<Base>
和shared_ptr<Derived>
是不相关的类型,除了可以从shared_ptr<Derived>
隐式创建shared_ptr<Base>
shared_ptr<Derived>
。
This creation adds a reference count. 此创建将添加参考计数。
If you really, really want to avoid that reference count... 如果您真的要避免引用计数...
template<class T>
struct shared_ptr_view {
template<class D>
shared_ptr_view( std::shared_ptr<D>& sptr ):
vtable( get_vtable<D>() ),
ptr( std::addressof(sptr) )
{}
shared_ptr_view( shared_ptr_view const& ) = default;
shared_ptr_view() = default;
shared_ptr_view& operator=( shared_ptr_view const& ) = delete;
T* get() const { if(vtable) return vtable->get(ptr); return nullptr; }
void clear() const { if(vtable) vtable->clear(ptr); }
std::shared_ptr<T> copy() const { if(vtable) return vtable->copy(ptr); return {} }
operator std::shared_ptr<T>() const { return copy(); }
T* operator->() const { return get(); }
T& operator*() const { return *get(); }
explicit operator bool() const { return get(); }
std::size_t use_count() const { if (vtable) return vtable->use_count(ptr); return 0; }
private:
struct vtable_t {
T*(*get)(void*) = 0;
std::shared_ptr<T>(*copy)(void*) = 0;
void(*clear)(void*) = 0;
std::size_t(*use_count)(void*) = 0;
};
vtable_t const* vtable = 0;
void* ptr = 0;
template<class D>
static vtable_t create_vtable() {
return {
[](void* ptr)->T*{ return static_cast<std::shared_ptr<D>*>(ptr)->get(); },
[](void* ptr)->std::shared_ptr<T>{ return *static_cast<std::shared_ptr<D>*>(ptr); },
[](void* ptr){ static_cast<std::shared_ptr<D>*>(ptr)->reset(); },
[](void* ptr){ return static_cast<std::shared_ptr<D>*>(ptr)->use_count(); }
};
}
template<class D>
static vtable_t const* get_vtable() {
static const auto vtable = create_vtable<D>();
return &vtable;
}
};
then 然后
void doGeneric( shared_ptr_view<Base> ptr ) {
ptr->run();
std::cout << " Ref count: " << ptr.use_count() << std::endl;
}
does not increase the reference count. 不会增加参考计数。 I think it is raw insanity.
我认为这是原始的疯狂。
shared_ptr_view.clear()
works, but shared_ptr_view.reset(T*)
cannot: a shared_ptr_view<Derived>
cannot be reset to point to a Base*
. shared_ptr_view.clear()
可以工作,但是shared_ptr_view.reset(T*)
不能: shared_ptr_view<Derived>
不能重置为指向Base*
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.