- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
众所周知,我们可以使用纯虚析构函数,如下所示:
struct A {
virtual ~A() = 0;
};
A::~A() {}
struct B : A {};
因为标准在 10.4 [class.abstract] p2
A pure virtual function need be defined only if called ... with (12.4 [class.dtor])
之后在 12.4 [class.dtor] p9
A destructor can be declared virtual (10.3) or pure virtual (10.4); if any objects of that class or any derived class are created in the program, the destructor shall be defined.
这意味着上面的代码是完全有效的 - A::~A
可能是纯虚拟的,它被定义,B::~B
隐式调用 A::~A
.
到目前为止,还不错。
然后我阅读了 10.4 [class.abstract] p6
:
Member functions can be called from a constructor (or destructor) of an abstract class; the effect of making a virtual call (10.3) to a pure virtual function directly or indirectly for the object being created (or destroyed) from such a constructor (or destructor) is undefined.
但这正是我们在这里所做的 - 我们从析构函数中调用纯虚函数 A::~A
。
那么,是不是存在某种矛盾?
最佳答案
没有收缩。
你调用了A
的虚析构函数来自 B
的析构函数. A
析构函数不是 B
的成员.
标准 § 9.3
成员函数
Functions declared in the definition of a class, excluding those declared with a friend specifier ( 11.3 ), are called member functions of that class
标准规定,当从抽象类析构函数/构造函数(在您的情况下,将是类 A
)对其自身的纯函数之一进行虚拟调用时,存在未定义的行为虚拟成员函数。
[...] Member functions can be called from a constructor (or destructor) of an abstract class [...];
你对析构函数的引用是这样的:
关于c++ - 如果从析构函数调用纯虚函数是UB,为什么我们可以使用纯虚析构函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24229131/
我开始考虑在我 future 的项目或重构中实现控制反转容器,我想知道在正确设计依赖项时哪些原则(除了 GoF 模式)可能需要牢记在心。假设我需要构建一个简单的控制台应用程序,如果它可以访问互联网,它
假设我有一个 RxC contingency table 。这意味着有 R 行和 C 列。我想要一个维度为 RC × (R + C − 2) 的矩阵 X,其中包含行的 R − 1 “主效应”以及列的
我正在尝试使用 DKMS 为正在运行的内核 (4.4) 构 build 备树覆盖。我天真的 Makefile 如下: PWD := $(shell pwd) dtbo-y += my-awsome-o
我有一个 sencha touch 项目。我是用 phonegap 2.9 构建的,并且可以正常工作 device.uuid 返回到设备 ID。当我尝试使用 3.1 device.uuid 构建时抛出
我在安装了 Xcode 4.5.1 的 Mt Lion 上运行。 默认情况下,当我构建并部署到 iOS 5.1 设备时,显示会在我旋转设备时旋转,但当我部署到 iOS 6 模拟器或运行 iOS 的 i
我正在尝试使用 Google Analytics Reporting API v4 构建多折线图。 一张图表,其中我按每天的 session 计数为每个设备(台式机/平板电脑/移动设备)设置了一条线。
我一生都无法使用 xcode 组织者“自动设备配置”中的“团队配置配置文件”在 xcode 4.0.1 中将我的应用程序构建到我的 iPad 上。 该应用程序完美地构建到模拟器,但当我构建到 iPad
我是一名优秀的程序员,十分优秀!