- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个问题在这里已经有了答案:
How to enable_shared_from_this of both parent and derived
(4 个回答)
1年前关闭。
不能std::shared_from_this
被其派生类继承?
为什么这段代码不能编译(检查 http://cpp.sh/7llcr )?我已经仔细阅读了文档( https://en.cppreference.com/w/cpp/memory/enable_shared_from_this ),但仍然找不到任何线索。我想了很久。我将非常感激能在这个问题上得到一些帮助。
这是无法编译的代码片段:
// enable_shared_from_this example
#include <iostream>
#include <memory>
struct C : std::enable_shared_from_this<C> {};
struct D : public C {};
int main () {
std::shared_ptr<D> foo, bar;
foo = std::make_shared<D>();
bar = foo->shared_from_this();
return 0;
}
In file included from /usr/include/c++/4.9/bits/shared_ptr.h:52:0,
from /usr/include/c++/4.9/memory:82,
from 3:
/usr/include/c++/4.9/bits/shared_ptr_base.h: In instantiation of 'std::__shared_ptr<_Tp, _Lp>& std::__shared_ptr<_Tp, _Lp>::operator=(std::__shared_ptr<_Tp1, _Lp>&&) [with _Tp1 = C; _Tp = D; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]':
/usr/include/c++/4.9/bits/shared_ptr.h:299:4: required from 'std::shared_ptr<_Tp>& std::shared_ptr<_Tp>::operator=(std::shared_ptr<_Tp1>&&) [with _Tp1 = C; _Tp = D]'
14:7: required from here
/usr/include/c++/4.9/bits/shared_ptr_base.h:1001:4: error: no matching function for call to 'std::__shared_ptr<D, (__gnu_cxx::_Lock_policy)2u>::__shared_ptr(std::remove_reference<std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>&>::type)'
__shared_ptr(std::move(__r)).swap(*this);
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:1001:4: note: candidates are:
/usr/include/c++/4.9/bits/shared_ptr_base.h:1145:7: note: std::__shared_ptr<_Tp, _Lp>::__shared_ptr(const std::__weak_ptr<_Tp, _Lp>&, std::nothrow_t) [with _Tp = D; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]
__shared_ptr(const __weak_ptr<_Tp, _Lp>& __r, std::nothrow_t)
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:1145:7: note: candidate expects 2 arguments, 1 provided
/usr/include/c++/4.9/bits/shared_ptr_base.h:1087:2: note: template<class _Alloc, class ... _Args> std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::_Sp_make_shared_tag, const _Alloc&, _Args&& ...)
__shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a,
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:1087:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/bits/shared_ptr_base.h:1001:4: note: cannot convert 'std::move<std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>&>((* & __r))' (type 'std::remove_reference<std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>&>::type {aka std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>}') to type 'std::_Sp_make_shared_tag'
__shared_ptr(std::move(__r)).swap(*this);
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:967:17: note: constexpr std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::nullptr_t) [with _Tp = D; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u; std::nullptr_t = std::nullptr_t]
constexpr __shared_ptr(nullptr_t) noexcept
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:967:17: note: no known conversion for argument 1 from 'std::remove_reference<std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>&>::type {aka std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>}' to 'std::nullptr_t'
/usr/include/c++/4.9/bits/shared_ptr_base.h:963:2: note: template<class _Tp1> std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::auto_ptr<_Up>&&)
__shared_ptr(std::auto_ptr<_Tp1>&& __r);
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:963:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/bits/shared_ptr_base.h:1001:4: note: 'std::remove_reference<std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>&>::type {aka std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>}' is not derived from 'std::auto_ptr<_Up>'
__shared_ptr(std::move(__r)).swap(*this);
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:951:2: note: template<class _Tp1, class _Del> std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::unique_ptr<_Up, _Ep>&&)
__shared_ptr(std::unique_ptr<_Tp1, _Del>&& __r)
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:951:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/bits/shared_ptr_base.h:1001:4: note: 'std::remove_reference<std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>&>::type {aka std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>}' is not derived from 'std::unique_ptr<_Tp, _Dp>'
__shared_ptr(std::move(__r)).swap(*this);
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:939:11: note: template<class _Tp1> std::__shared_ptr<_Tp, _Lp>::__shared_ptr(const std::__weak_ptr<_Tp1, _Lp>&)
explicit __shared_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:939:11: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/bits/shared_ptr_base.h:1001:4: note: 'std::remove_reference<std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>&>::type {aka std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>}' is not derived from 'const std::__weak_ptr<_Tp>'
__shared_ptr(std::move(__r)).swap(*this);
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:931:2: note: template<class _Tp1, class> std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::__shared_ptr<_Tp1, _Lp>&&)
__shared_ptr(__shared_ptr<_Tp1, _Lp>&& __r) noexcept
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:931:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/bits/shared_ptr_base.h:929:31: error: no type named 'type' in 'struct std::enable_if<false, void>'
template<typename _Tp1, typename = typename
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:922:7: note: std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::__shared_ptr<_Tp, _Lp>&&) [with _Tp = D; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]
__shared_ptr(__shared_ptr&& __r) noexcept
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:922:7: note: no known conversion for argument 1 from 'std::remove_reference<std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>&>::type {aka std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>}' to 'std::__shared_ptr<D, (__gnu_cxx::_Lock_policy)2u>&&'
/usr/include/c++/4.9/bits/shared_ptr_base.h:918:2: note: template<class _Tp1, class> std::__shared_ptr<_Tp, _Lp>::__shared_ptr(const std::__shared_ptr<_Tp1, _Lp>&)
__shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:918:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/bits/shared_ptr_base.h:916:31: error: no type named 'type' in 'struct std::enable_if<false, void>'
template<typename _Tp1, typename = typename
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:912:7: note: std::__shared_ptr<_Tp, _Lp>::__shared_ptr(const std::__shared_ptr<_Tp, _Lp>&) [with _Tp = D; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]
__shared_ptr(const __shared_ptr&) noexcept = default;
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:912:7: note: no known conversion for argument 1 from 'std::remove_reference<std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>&>::type {aka std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>}' to 'const std::__shared_ptr<D, (__gnu_cxx::_Lock_policy)2u>&'
/usr/include/c++/4.9/bits/shared_ptr_base.h:908:2: note: template<class _Tp1> std::__shared_ptr<_Tp, _Lp>::__shared_ptr(const std::__shared_ptr<_Tp1, _Lp>&, _Tp*)
__shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r, _Tp* __p) noexcept
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:908:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/bits/shared_ptr_base.h:1001:4: note: candidate expects 2 arguments, 1 provided
__shared_ptr(std::move(__r)).swap(*this);
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:903:9: note: template<class _Deleter, class _Alloc> std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::nullptr_t, _Deleter, _Alloc)
__shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:903:9: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/bits/shared_ptr_base.h:1001:4: note: cannot convert 'std::move<std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>&>((* & __r))' (type 'std::remove_reference<std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>&>::type {aka std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>}') to type 'std::nullptr_t'
__shared_ptr(std::move(__r)).swap(*this);
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:898:2: note: template<class _Deleter> std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::nullptr_t, _Deleter)
__shared_ptr(nullptr_t __p, _Deleter __d)
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:898:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/bits/shared_ptr_base.h:1001:4: note: cannot convert 'std::move<std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>&>((* & __r))' (type 'std::remove_reference<std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>&>::type {aka std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>}') to type 'std::nullptr_t'
__shared_ptr(std::move(__r)).swap(*this);
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:889:2: note: template<class _Tp1, class _Deleter, class _Alloc> std::__shared_ptr<_Tp, _Lp>::__shared_ptr(_Tp1*, _Deleter, _Alloc)
__shared_ptr(_Tp1* __p, _Deleter __d, _Alloc __a)
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:889:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/bits/shared_ptr_base.h:1001:4: note: mismatched types '_Tp1*' and 'std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>'
__shared_ptr(std::move(__r)).swap(*this);
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:880:2: note: template<class _Tp1, class _Deleter> std::__shared_ptr<_Tp, _Lp>::__shared_ptr(_Tp1*, _Deleter)
__shared_ptr(_Tp1* __p, _Deleter __d)
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:880:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/bits/shared_ptr_base.h:1001:4: note: mismatched types '_Tp1*' and 'std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>'
__shared_ptr(std::move(__r)).swap(*this);
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:870:11: note: template<class _Tp1> std::__shared_ptr<_Tp, _Lp>::__shared_ptr(_Tp1*)
explicit __shared_ptr(_Tp1* __p)
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:870:11: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/bits/shared_ptr_base.h:1001:4: note: mismatched types '_Tp1*' and 'std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>'
__shared_ptr(std::move(__r)).swap(*this);
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:865:17: note: constexpr std::__shared_ptr<_Tp, _Lp>::__shared_ptr() [with _Tp = D; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]
constexpr __shared_ptr() noexcept
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:865:17: note: candidate expects 0 arguments, 1 provided
最佳答案
它是继承的:
// enable_shared_from_this example
#include <iostream>
#include <memory>
struct C : std::enable_shared_from_this<C> {};
struct D : public C {};
int main () {
std::shared_ptr<D> foo;
foo = std::make_shared<D>();
std::shared_ptr<C> bar = foo->shared_from_this();
return 0;
}
shared_from_this
在
class C
: 所以对象返回
std::shated_ptr<C>
.没有惊喜。您仍然可以将其转换为
std::shared_ptr<D>
与
std::dynamic_pointer_cast
.
关于c++ - "std::shared_from_this"不能被它的派生类继承吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62317934/
在尝试研究我的问题时,我已经阅读了许多有关 shared_from_this、bad_weak_ptr 异常和多重继承的堆栈溢出文章。他们都建议您从 enable_shared_from_this 继
如您所知,不可能在对象的构造函数中使用 std::enable_shared_from_this 和 shared_from_this() 对,因为包含该类的 shared_pointer 尚不存在。
我正在尝试使用 shared_from_this 函数创建指向 this 的共享指针。 #include #include class foo { public: virtual void
有时一个类需要获得一个shared_ptr对自己。实现此目的的一种方法(当您知道类的对象将始终存在于 shared_ptr 中时)是从 std::enable_shared_from_this 派生。
我正在编写具有类似 Blender 功能的基于 Qt 的应用程序。 它由一个“框架”组成,即 GUI + 插件系统和插件。插件是带有基本上可以创建和显示的对象(例如 Sphere、Box 等)的 Qt
有人能用几句简洁的话总结一下如何 boost shared_from_this<>()应该使用智能指针,特别是从使用绑定(bind)函数在 io_service 中注册处理程序的角度来看。 编辑:一些
我有一个资源管理器,就像 Andrei Alexandrescu 在 Modern C++ Design 一书中提出的那样,它遵循基于策略的设计。不过我遇到了麻烦,因为我的资源管理器需要能够通过 sh
我必须在创建容器时在容器中注册一个对象。如果没有智能指针,我会使用这样的东西: a_class::a_class() { register_somewhere(this); } 使用智能指针我
这个问题在这里已经有了答案: How to enable_shared_from_this of both parent and derived (4 个回答) 1年前关闭。 不能std::share
我有一些派生自 boost::shared_from_this 的类 - 我如何在 Qt 中执行等效的操作? (无需重新实现类似的 shared_from_this;我更喜欢无继承解决方案) 最佳答案
#include #include #include #include class A: public boost::enable_shared_from_this { typedef
我想使用 shared_from_this() 从通过引用传递的对象中获取 std::shared_ptr。但是,我希望对可能不在共享指针中或者甚至可能不是动态分配的对象保持健壮。 在调用 share
我想用一个包含不可复制成员的类来启动一个线程。为了与线程通信,我想在将对象移入线程之前从该对象创建一个共享指针。 移动构造函数是否使共享指针无效?如果是这样,那么优雅的 c++ 方法是什么?一种解决方
假设我们有一个类 Foo 定义如下: // foo.hpp class Foo; using FooCallback = std::function ins)>; class Foo : public
当从继承自 enable_shared_from_this 的类型中调用 shared_from_this 时,如果 this 当前未被 shared_ptr 对象持有(通常是应用程序崩溃),可能会发
当我这样做时出现异常:std::bad_weak_ptr->shared_from_this() template class painter_record_t { ....... private:
我想从继承自 enable_shared_from_this 的类 A 创建一个仿函数,它适用于这样的类: class A: public std::enable_shared_from_this {
在this source file有两个类:tcp_connection 和 tcp_server。在我看来,我已经选择了相关的代码位,但您可能想要引用完整的源代码以获取更多信息。 class tcp
在我的代码中,我使用 boost::threads我有一个通过名为 fnThread() 的成员函数运行线程的类.在此 fnThread() 中,我想创建一个 shared_from_this() 并
这个问题在这里已经有了答案: How to break shared_ptr cyclic reference using weak_ptr (1 个回答) 关闭 6 年前。 我有两个类,A 和 B
我是一名优秀的程序员,十分优秀!