[英]Please explain the sequence of execution in the following code
I am trying to solve a quiz and came across this question. 我正在尝试解决一个测验,并遇到了这个问题。
Please explain what happens internally that leads to this Output 请解释导致此输出的内部情况
class B; class A { friend class B; public: ~A() { B boj(); cout << "object A destructor " << endl; } }; class B { public: ~B() { cout << "object B destructor " << endl; } }; int main() { A a; A aobj(); B bobj(); }
The output is: 输出为:
Object A destructor
对象A析构函数
I am trying to create a program where a user enters if he wishes to add another record, and if yes then create a new object for that record. 我正在尝试创建一个程序,如果用户希望添加另一条记录,则在该程序中输入,如果是,则为该记录创建一个新对象。
So if I am including constructors, then how do I create a new object every time the user wants? 因此,如果我包括构造函数,那么如何在每次用户需要时创建一个新对象?
(If I give a predefined size to the array of object, then constructor will be called, say 50 times and initialize all 50 objects, while the user may only want to enter less). (如果我给对象数组指定了预定义的大小,则构造函数将被调用50次,并初始化所有50个对象,而用户可能只希望输入更少的内容)。
First, a
is constructed. 首先,
a
被构造。 Then, a
is destroyed. 然后,
a
被销毁。
The final two declarations in main
, and the declaration inside ~B()
, are all local function declarations and therefore don't "do" anything. main
的最后两个声明以及〜B ~B()
内的声明都是本地函数声明,因此不做任何事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.