- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我找到的关于 enable_shared_from_this
的例子显示它通过继承使用。例如:
struct Good : enable_shared_from_this<Good> {
shared_ptr<Good> getptr() {
return shared_from_this();
}
};
int main() {
// Good: the two shared_ptr's share the same object
shared_ptr<Good> gp1(new Good);
shared_ptr<Good> gp2 = gp1->getptr();
cout << "gp2.use_count() = " << gp2.use_count() << endl;
}
我这一天已经收到很多关于从标准库继承的危险的警告。这段代码似乎确实存在这些危险,例如:
struct A : enable_shared_from_this<A> {};
struct B : enable_shared_from_this<B> {};
如果我想创建 struct C : A, B {};
,关键点显然是 C::shared_from_this()
。显然我们可以解决这个问题,但存在一些固有的复杂性。
所以我的问题是,有没有一种方法可以将 enable_shard_from_this
用作 has-a 关系而不是 is-a 关系?
最佳答案
is there a way to use
enable_shard_from_this
as a has-a relationship instead of an is-a relationship?
没有。
enable_shared_from_this
应该用作基类,因此在这种情况下,盲目地应用适用于其他情况的指南是行不通的。
即使有充分的理由想要这样做(但没有),它也不会奏效。导致 enable_shared_from_this
的魔法基地与shared_ptr
共享所有权拥有派生对象的对象通过检查继承来工作。
enable_shared_from_this
无论如何都不对 IS-A 关系建模,因为它没有根据虚函数定义的接口(interface)。 IS-A 表示扩展基接口(interface)的派生类型,但这里不是这种情况。 Good
IS-NOT-A enable_shared_from_this<Good>
.
即使用继承并不总是意味着 IS-A 关系。
- enable_shared_from_this doesn't have a virtual destructor
除非您计划通过指向 enable_shared_from_this
的指针删除对象,否则虚拟析构函数是无关紧要的。基类,这太疯狂了。没有理由绕过 Good
作为指向 enable_shared_from_this<Good>
的指针基类,更没有理由使用 delete
在该基指针上(通常类型会在创建后立即存储在 shared_ptr<Good>
中,因此您根本不会使用 delete
)。
enable_shared_from_this
是混合类型,而不是抽象基础。它提供了 shared_from_this
(很快就会成为 weak_from_this
)成员(member),仅此而已。您不应该将其用作抽象基类或接口(interface)类型,也不应使用基类来访问派生类型的多态行为。事实上,它根本没有虚函数,而不仅仅是没有虚析构函数,应该告诉你这一点。
此外,作为 n.m.上面评论过,析构函数是 protected ,因此您不能通过基类删除它,即使您尝试过( protected 析构函数是防止这种类型的混合类滥用的惯用方法非多态基类)。
- The
enable_shared_from_this
destructor destroys*this
, meaning it must always be the last destructor called
嗯?不确定你的意思,但它不负责销毁除自身和它自己的数据成员之外的任何东西。
- Inheritance from two classes that both inherit from
enable_shared_from_this
can become a bit of a sticking point
它应该可以正常工作(尽管如果没有一个明确的基类是 enable_shared_from_this
的特化,您可能无法获得神奇的所有权共享)。 GCC 标准库有一个错误(现已修复),它无法编译,而不仅仅是无法共享所有权,但这不是 enable_shared_from_this
的问题。 .
关于c++ - enable_shared_from_this 可以在没有继承的情况下使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34042589/
类中使用shared_ptr()的问题 当我们先定义一个指针,然后再用这个指针构造两个智能指针 int main() { int* pt = new int(); std::shared_
我有两个类A和B,其中B是A的子类。我需要两个类都可以使用std::enable_shared_from_this。 我尝试了这个: #include #include #include cla
我正在尝试在模板函数中使用 std::enabled_shared_from_this 函数,但在类中调用 shared_from_this() 时不断收到消息 bad_weak_ptr 错误。 te
我有以下类结构用于管理具有不同原型(prototype)的回调: class MethodHandlerBase: public std::enable_shared_from_this{ publi
我的类继承自多个基类,其中之一是 std::enable_shared_from_this .必须是第一垒吗? 假设以下示例代码: struct A { ~A(); }; struct B { ~B(
我找到的关于 enable_shared_from_this 的例子显示它通过继承使用。例如: struct Good : enable_shared_from_this { shared_p
在公开继承 enable_shared_from_this 并初始化类的对象后,在调用该类的另一个函数时,我在 Visual Studio 中调试时仍然可以看到 enable_shared_from_
我在阅读 Boost.Asio 示例时遇到了 enable_shared_from_this,在阅读了文档之后,我仍然不知道如何正确使用它。有人可以给我一个例子和解释什么时候使用这个类是有意义的。 最
这个问题在这里已经有了答案: What is the usefulness of `enable_shared_from_this`? (6 个答案) 关闭 8 年前。 我正在查看 shared_p
当我继承std::enable_shared_from_this,但是我创建了一个unique_ptr,std::enable_shared_from_this里面的weak_ptr也会被初始化吗当我
这个问题在这里已经有了答案: What is the usefulness of `enable_shared_from_this`? (6 个答案) 关闭 6 年前。 我是 C++11 的新手,我
我有一个对象 (Z),它派生自另外两个对象(A 和 B)。 A 和 B 都派生自 enable_shared_from_this<> ,分别enable_shared_from_this和 enabl
有没有办法阻止 shared_from_this() 调用堆栈分配的对象? 基类列表中的 enable_shared_from_this<> 是类用户的强指标,但有没有办法强制正确使用? 示例代码:
为什么我应该使用 enable_shared_from_this 因为我也可以通过简单的赋值获得相同的效果。 struct A : std::enable_shared_from_this { std
我正在使用 enable_shared_from_this然后从 Base 继承.尝试使用 shared_from_this() 时在 Derived的构造函数(不是初始化列表),我得到一个异常。原来
我有一个继承自 enable_shared_from_this 的类型,以及从该类型继承的另一种类型。现在我不能使用 shared_from_this 方法,因为它返回基类型,并且在特定的派生类方法中
我在阅读 Boost.Asio 示例时遇到了 enable_shared_from_this,在阅读了文档之后,我仍然不知道应该如何正确使用它。有人可以给我一个例子和解释什么时候使用这个类是有意义的。
要点回顾 继承自 std::enable_shared_from_this<T> 的类能够在其自身实例中通过 std::shared_from_this 方法创建一个指向自己的
这个问题在这里已经有了答案: Double inheritance of enable_shared_from_this (3 个回答) 1年前关闭。 当我尝试运行此代码时,我得到 bad_weak_
我不知道这个问题是否适合 SO,但我们开始了: 启用 shared_from_this 时,是否会增加可执行文件的大小?解决 shared_from_this() 调用是否也需要太多时间? 查看文档,
我是一名优秀的程序员,十分优秀!