直接上例子
#include <iostream>
class Base{
public:
Base(const char* p_szName):m_szName(p_szName){std::cout<<m_szName<<",";}
virtual ~Base(){std::out<<"~"<<m_szName<<",";}
private:
const char* m_szName;
}
class Derived:public Base{
public:
Derived(const char* p_szName):m_szName(p_szName),
m_objA((const char*)"A"),
m_objB((const char*)"B"),
Base((const char*)"C")
{
std::cout<<m_szName<<",";
}
virtual ~Derived(){
std::out<<"~"<<m_szName<<",";
}
private:
const char* m_szName;
Base m_objB;
Base m_objA;
};
int main()
{
Base *p = new Derived("D");
delete p;
return 0;
}
- 构造函数的调用顺序: 基类 >> 对象 >> 派生类,析构函数相反;
- 构造函数里初始化列表初始化顺序由成员变量的声明顺序决定。