c++类继承,构造函数和析构函数调用顺序

来源:互联网 发布:淘宝怎么搜阿普唑仑 编辑:程序博客网 时间:2024/06/05 02:31


class A

{

 public: A()

{

 printf("A\n"); }

 ~A()

 {

 printf("~A\n");

 }

};

class B : public A

{

 public: B()

{

printf("B\n");

}

 ~B()

 {

 printf("~B\n");

 }

};

 int _tmain(int argc, _TCHAR* argv[]) { B *b= new B(); delete b; return 0; }

运行结果:

A

B

~B

 ~A

class A

{

 public: A()

{

 printf("A\n"); }

 ~A()

 {

 printf("~A\n");

 }

};

class B : public A

{

 public: B()

{

printf("B\n");

}

 ~B()

 {

 printf("~B\n");

 }

};

int _tmain(int argc, _TCHAR* argv[]) { B *b= new B(); A* a = (A*)b; delete a; return 0; }

运行结果:

A

B

~A

0 0
原创粉丝点击