现在的位置: 首页 > 综合 > 正文

vc类内包含类成员的总结

2013年09月26日 ⁄ 综合 ⁄ 共 445字 ⁄ 字号 评论关闭

程序:
#include <iostream>
using namespace std;

class A
{
public:
 A(int n = 0 ):m_nTest(n)
 {
  cout << "A constructor" << endl;
 }
//private:
 int m_nTest;
};

class B
{
public:
 B(A* p1):m_a(p1)
 {
  cout << "B constructor" << endl;
 }
private:
 A* m_a;
};

class B1:public B
{
public :
 B1():B(&a1)//,//a1(5)
 {
  cout << "B1 constructor" << endl;
  cout << a1.m_nTest << endl;;
 }
private:
 A a1;
};

int main()
{
 B1 b;
 return EXIT_SUCCESS;
}

总结:
当一个派生类里面包含了一个类类型的数据成员,其构造的时候,不管派生列表内有没有构造该成员,编译器都会自动调用其默认构造函数,在派生类进入其函数体内之前

抱歉!评论已关闭.