#include<stdio.h> class A { public: A(){ printf("A");} ~A(){ printf("~A");} }; class B : public A { public: B(){ printf("B");} ~B(){ printf("~B");} }; int main() { A *c = new B[2]; delete[] c; return 0; }
#include<stdio.h> class A { public: A(){ printf("A");} ~A(){ printf("~A");} }; class B : public A { public: B(){ printf("B");} ~B(){ printf("~B");} }; int main() { A *c = new B[2]; delete[] c; return 0; }
ABAB~A~A
ABAB~B~A~B~A
ABAB~B~A
ABAB~A~B~A~B
//p为指向基类的指针 delete [] p;
void * vec_delete ( //数组首地址 void *array, //元素大小 size_t elem_size, //元素个数 int elem_count, //析构函数指针 void (*destructor)(void *) );
class Base {
public:
Base() {printf("Base\n");}
~Base() {printf("~Base\n");}
int i = 1;
};
class A : virtual public Base
{
public:
A() { printf("A\n");}
~A(){ printf("~A\n");}
};
class B : virtual public Base
{
public:
B() { printf("B\n");}
~B(){ printf("~B\n");}
};
class C : public A, public B
{
public:
C() { printf("C\n");}
~C(){ printf("~C\n");}
};
int main()
{
C *c = new C[2];
/*
分别将如下各变体拿到注释外执行:
Base *p = c;
A *p = c;
B *p = c;
*/
printf("%p %p\n", p, c);
delete[] p;
}
delete p;
const static int N = 2; A* c = new B[N]; for (int i=0; i<N; ++i) { delete ((B*)c+i); }
ABAB~B~ATestCPP(1739,0x10012c3c0) malloc: *** error for object 0x1004067e8: pointer being freed was not allocated
B* p = new B[N]; delete[] p;
#include<stdio.h>
class A
{
public:
A(){ printf("A");}
~A(){ printf("~A");}
};
class B: public A
{
public:
B(){ printf("B");}
~B(){ printf("~B");}
};
int main()
{
A* c = new B[2];
delete[] c;
return 0;
ABAB~A~A
class A
{
public:
A(){ printf("A");}
virtual ~A(){ printf("~A");}
};
class B: public A
{
public:
B(){ printf("B");}
~B(){ printf("~B");}
};
int main()
{
A* c = new B[2];
delete[] c;
return 0;
class A
{
public:
A(){ printf("A");}
~A(){ printf("~A");}
};
class B: public A
{
public:
B(){ printf("B");}
~B(){ printf("~B");}
};
int main()
{
B* c = new B[2];
delete[] c;
return 0;
ABAB~B~A~B~A
#include<stdio.h>
class A
{
public:
A(){ printf("A");}
virtual ~A(){ printf("~A");}
};
class B: public A
{
public:
B(){ printf("B");}
~B(){ printf("~B");}
};
int main()
{
B* c = new B[2];
delete[] c;
return 0;
继承和派生中的 类型兼容规则
那么和多态的关系呢?
#include<stdio.h> class A { public: A(){ printf(“A”);} ~A(){ printf(“~A”); }; // 基类A 析构函数不是虚函数 class B:public A { public; B(){ printf(“B”);} ~B(){ printf(“~B”);} };//派生类 B int main() { A*c = new B[2]; //定义了一个 A类的对象指针 ,只不过用派生类B来替代 也就是派生类动态分配的内存 //初始化从基类开始,然后是派生类 ABAB delete[] c;// 释放内存,通过的是 基类指针,调用的是基类的析构函数,那么派生类对象中的动态内存分配的空间没有释放,造成了内训泄露 //派生类对象的内存 在对象消失后,既不能被本程序使用,也没有被释放。 //对于内存需求量大,长期连续运行的内存来说,如果持续发生这样的错误是很危险的,最终导致内存不足引起程序的终止 return 0; }
// 1 /* #include<stdio.h> class A { public: A(){ printf("A");} ~A(){ printf("~A");} }; class B: public A { public: B(){ printf("B");} ~B(){ printf("~B");} }; int main() { A* c = new B[2]; delete[] c;// ABAB~A~A return 0; } */ // 2 /* #include<stdio.h> class A { public: A(){printf("A");} virtual ~A(){printf("~A");} }; class B: public A { public: B(){printf("B");} ~B(){printf("~B");} }; int main() { A* c = new B[2]; delete[] c; // ABAB~B~A~B~A return 0; } */ //3 /* #include<stdio.h> class A { public: A(){printf("A");} ~A(){printf("~A");} }; class B: public A { public: B(){printf("B");} ~B(){printf("~B");} }; int main() { B* c =new B[2]; delete[] c;// ABAB~B~A~B~A return 0; } */ //4 /* #include<stdio.h> class A { public: A(){ printf("A");} virtual ~A(){ printf("~A");} }; class B: public A { public: B(){ printf("B");} ~B(){ printf("~B");} }; int main() { B* c = new B[2]; delete[] c;//ABAB~B~A~B~A return 0; } */ // 基类的析构函数是虚函数,delete销毁对象时,执行顺序:派生类的析构函数->对象成员的析构函数->基类的析构函数