- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我定义了一个类似于下面的类:
class A : std::enable_shared_from_this<A> {
public:
static std::shared_ptr<A> create() {
return std::shared_ptr<A>(new A());
}
void f() {
shared_from_this();
}
private:
A() { }
};
它的用法类似于:
std::shared_ptr<A> pt = A::create();
pt->f();
尽管在创建 shared_ptr pt
之后调用了 shared_from_this(),但调用 f()
仍然会导致 bad_weak_ptr
抛出异常。在 gdb
中运行它可以确认异常是在调用 f()
时抛出的,而不是在我未包含在此处的一些由讲师调用的代码中抛出的。
最佳答案
std::enable_shared_from_this<A>
必须是公共(public) 基地。当 shared_ptr
构造函数看到你从 enable_shared_from_this
派生, 它在 enable_shared_from_this
中存储了自身的弱拷贝目的。如果继承不是公共(public)的,则不会发生这种情况,并且 weak_ptr
存储在 enable_shared_from_this
中将为空,导致 bad_weak_ptr
shared_from_this
时异常后来尝试构建一个 shared_ptr
从它。
关于c++ - 对象构造后 shared_from_this 中的 std::bad_weak_ptr 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52194823/
在尝试研究我的问题时,我已经阅读了许多有关 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
我是一名优秀的程序员,十分优秀!