[英]Please explain why this code prints 02002 and not 00022
我不明白為什么這段代碼打印 02002 而不是 00022。據我所知,構造函數的順序是:首先是基類,然后是成員對象(按初始化列表的順序),然后是實際類的構造函數。 我在這里有什么問題?
#include <iostream>
class A
{
public:
A(int n = 0)
: m_i(n)
{
std::cout << m_i;
++m_i;
}
protected:
int m_i;
};
class B
: public A
{
public:
B(int n = 5) : m_a(new A[2]), m_x(++m_i) { std::cout << m_i; }
~B() { delete[] m_a; }
private:
A m_x;
A* m_a;
};
int main()
{
B b;
std::cout << std::endl;
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.