- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我知道这个问题有明确的答案:先调用基类构造函数,再调用派生类构造函数。
但是“叫”这个词我不是很懂。是构造函数开始使用,还是构造函数使用结束?换句话说,下面的代码有两种可能的顺序:
BaseClass 构造函数开始 -> BaseClass 构造函数完成 -> DerivedClass 构造函数开始 -> DerivedClass 构造函数完成。
DerivedClass 构造函数开始 -> BaseClass 构造函数开始 -> BaseClass 构造函数完成 -> DerivedClass 构造函数完成。
哪一个应该是正确的顺序?如果 1 是正确的,编译器如何知道在我们初始化 DerivedClass 实例之前调用 BaseClass 构造函数?
似乎情况 2 是正确的:“被调用”应该意味着构造函数的完成。后续问题是析构函数如何?我知道标准答案是“首先调用派生类的析构函数”。那么哪个是正确的顺序:
谢谢
class BaseClass {
public:
BaseClass() {
cout << "BaseClass constructor." << endl;
}
};
class DerivedClass : public BaseClass {
public:
DerivedClass() : BaseClass() {
cout << "DerivedClass constructor." << endl;
}
};
int main() {
DerivedClass dc;
}
最佳答案
[class.base.init]/10,强调我的:
In a non-delegating constructor, initialization proceeds in the following order:
First, and only for the constructor of the most derived class, virtual base classes are initialized [...]
Then, direct base classes are initialized in declaration order as they appear in the base-specifier-list (regardless of the order of the mem-initializers).
Then, non-static data members are initialized in the order they were declared in the class definition (again regardless of the order of the mem-initializers).
Finally, the compound-statement of the constructor body is executed.
也就是说,派生类的构造函数首先被“调用”,但在它的复合语句(它的函数体)之前,基类构造函数必须完成。
我们可以看到这个顺序的一种方法是在派生类的 ctor 中使用 function-try-block:
#include <iostream>
struct Base {
Base() { throw "Base throwing\n"; }
};
struct Derived : Base{
Derived()
try : Base()
{}
catch(char const* p) {
std::cout << p;
}
};
int main() {
try { Derived d; }catch(...){}
}
function-try-block 可以捕获在基类和成员初始化期间发生的异常。隐式传播异常:由于无法构造/初始化基/成员,因此无法构造/初始化(派生)对象。
对于析构函数,[class.dtor]/8
After executing the body of the destructor and destroying any automatic objects allocated within the body, a destructor for class
X
calls the destructors forX
’s direct non-variant non-static data members, the destructors forX
’s direct base classes and, ifX
is the type of the most derived class, its destructor calls the destructors forX
’s virtual base classes.All destructors are called as if they were referenced with a qualified name, that is, ignoring any possible virtual overriding destructors in more derived classes. Bases and members are destroyed in the reverse order of the completion of their constructor.
如果析构函数是虚拟的,编译器(有时)无法知道必须在析构站点/翻译单元调用哪些析构函数(最派生类型及其基类)。因此,析构函数本身必须调用基类和成员的析构(至少对于通过动态调度调用的虚拟析构函数)。
关于c++ - 基类构造函数真的在派生类构造函数之前被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25997329/
这个问题在这里已经有了答案: 关闭 12 年前。 Possible Duplicate: Why can't Python handle true/false values as I expect?
我是不是遗漏了什么或者这是 ruby 中的错误? a = %w(foo bar baz) a.include? "foo" # => true a.size == 3
从 Modelica 文档来看,注释 Evaluate 似乎只对参数有影响: https://build.openmodelica.org/Documentation/ModelicaReferenc
为了避免嵌套的 if 语句并提高可读性,我想创建一个switch(true){ ... } Coldfusion 中的声明。我在 php 中经常使用这个,但是当我在 Coldfusion 中尝试这个时
嗨,我正在尝试处理 ajax json 响应 这是我的代码 success: function (j) { switch(true) { case (j.cho
我之前在我的 TF 代码中使用过这个: count = "${var.whatever == "true" ? 1 : 0}" 这非常适合我想要使用的东西。但是,我正在考虑如何最好地使用类似于说的
我之前在我的 TF 代码中使用过这个: count = "${var.whatever == "true" ? 1 : 0}" 这非常适合我想要使用的东西。但是,我正在考虑如何最好地使用类似于说的
这个问题在这里已经有了答案: How can I return pivot table output in MySQL? (10 个答案) 关闭 5 年前。 我正在尝试构建一个以唯一列值作为列名的表
我制作了一个简单的 JDialog,其中包含一个标签和一个按钮,它基本上相当于信息对话框。所以在对话框中,有一个方法 display() 我在其中调用了 setVisible(true) 五次。 据我
在 bash 4.2.8(1)-release (x86_64-pc-linux-gnu) 在 Ubuntu 11.04 上这个命令 [ $(wc -l /var/www/some.log|cut -
我正在使用 c 语言进行并发处理,我有一个进程池。为此,我让每个 child 都在一个 While (True) 循环中。为了杀死 child ,我正在使用一个全局变量和一个信号处理程序来修改它来打破
我正在尝试选择填写了字段的数据库条目。数据库有两种插入数据的方式,一种输入评论,一种不输入,我希望只选择填写了评论的行。 $requete = "SELECT * FROM daysoff WHER
如何在 JavaMail session 中setDebug(true) 捕获流并在我的日志记录框架中使用它? (缺少下载源代码,更改接受流作为参数的方法,重新编译它,...) 更一般地说,Java
我是 JavaScript 的新手,我刚刚发现了我无法理解的奇怪行为: var magicVar = Math.sin; magicVar == true; // it returns false m
对此感到困惑。 在两台服务器上运行相同版本的 MySQL。 (从完全相同的 rpm 构建)- 沿线的某个地方,一些开发人员改变了一些东西...... 服务器 1: mysql> select ( no
我在查看 OpenSSL 中使用的一些预处理器宏时,从 crypto/stack/safestack.h 中发现了以下内容: #define CHECKED_STACK_OF(type, p) \
所以我遇到了一个问题,我的正则表达式看起来像这样:/true|false/。 当我检查单词 falsee 时,我从这个正则表达式中得到一个 true,有没有办法将它限制为确切的 true 或 fals
我正在对这个恶意 JavaScript 行进行一些试验:var undefined = true; JavaScript 中每个未初始化的变量都有 undefined 的值,这只是一个保存特殊值 'u
我想将 PHP 的微时间存储为我在 MySQL 中的时间戳。 我去过told最好用 DECIMAL 存储它,但我找不到理想的大小。 有谁知道 microtime(true) 返回的最大大小是多少,所以
在 PHP 中,当您在 URL 中包含诸如“var=true”之类的内容时,URL 中的“true”和“false”是否被转换为 boolean 变量,或者它们是否等于文本“true”还是“假”?例如
我是一名优秀的程序员,十分优秀!