一、继承的内存布局
(1)单一继承
class Point2D
{
public:
int _x;
int _y;
};
class Point3D : public Point2D
{
public:
int _z;
};
(2)多重继承
class Point4D : public Point3D
{
public:
int _h;
};
(3)虚继承
class Point3D : public virtual Point2D
{
public:
int _z;
};
class Vertex : public virtual Point2D
{
public:
Vertex * next;
};
class Vertex3D : public Point3D, public Vertex
{
private:
int mumble;
};
备注:在Vertex也能看到Point2D,但是实际上Vertex中的跟Pointe3D中的Point2D是同一个。
二、继承中虚表的布局
(1)单一继承
class Point2D
{
public:
virtual int speak(){return 0;};
virtual int clone(){return 0;};
private:
int _x;
int _y;
};
class Point3D : public /*virtual*/ Point2D
{
public:
int speak(){return 0;};
virtual int sayhi(){return 0;};
private:
int _z;
};
(2)多重继承
class Point2D
{
public:
virtual int speak(){return 0;};
virtual int clone(){return 0;};
private:
int _x;
int _y;
};
class Point3D
{
public:
virtual int say(){return 0;};
private:
int _z;
};
class Point4D : public Point3D, public Point2D
{
public:
int speak(){return 0;};
int clone(){return 0;};
int sayhi(){return 0;};
private:
int _h;
};
为什么这样继承,虚表布局是这样的?
class Point2D
{
public:
virtual int speak(){return 0;};
virtual int clone(){return 0;};
private:
int _x;
int _y;
};
class Point3D : public Point2D
{
public:
virtual int say(){return 0;};
private:
int _z;
};
class Point4D : public Point3D
{
public:
int speak(){return 0;};
int clone(){return 0;};
int say(){return 0;};
private:
int _h;
};
(3)虚继承
class Point2D
{
public:
virtual int speak(){return 0;};
virtual int clone(){return 0;};
private:
int _x;
int _y;
};
class Point3D : public virtual Point2D
{
public:
virtual int say(){return 0;};
private:
int _z;
};
class Vertex : public virtual Point2D
{
public:
virtual int act(){return 0;};
private:
Vertex * next;
};
class Vertex3D : public Point3D, public Vertex
{
public:
int speak(){return 0;};
int clone(){return 0;};
int say(){return 0;};
int act(){return 0;};
private:
int mumble;
};
三、构造函数
(1)构造函数
不能是虚函数,且如果基类的构造函数有member initialization list,子类需要将这个member list传给基类;如果没有这个list的话,可以不用显示调用基类的构造函数;
基类的构造函数先运行,然后才是子类的构造函数。
new对象时,会调用构造函数。
(2)默认构造函数
会自动调用成员变量的构造函数进行初始化。
class Point2D
{
public:
Point2D(){printf("constructor\n");}
~Point2D(){printf("destructor\n");}
private:
int _x;
int _y;
};
class Point3D
{
private:
Point2D pa;
};
int _tmain(int argc, _TCHAR* argv[])
{
Point3D* pa = new Point3D();
delete pa;
return 0;
}
四、析构函数
如果对象有析构函数,delete会调用对象的析构函数,并释放对象内存。如果类没有显示的提供析构函数,则会合成一个默认析构函数。
析构函数执行顺序:子类的析构函数先运行,再基类。
为什么要提供虚析构函数:如果基类的构造函数为非虚函数,则子类析构时,不会调用基类的析构函数,有可能会造成内存泄漏。如果子类的构造函数为非虚函数,则用基类的指针指向子类,再delete基类指针时,子类的析构函数不会被调用。
能否使用delete this?偶尔会看到代码里存在某个成员函数中出现delete this的情况。可以参考一下这篇文章:http://www.cppblog.com/xmli/archive/2009/08/18/93683.aspx( 结论是:在成员函数中调用delete this会造成指针错误;在析构函数中调用delete this,会导致死循环,堆栈溢出。)
默认的析构函数,只会销毁成员变量的内存,并不会调用其析构函数。
class Point2D
{
public:
Point2D(){printf("constructor\n");}
~Point2D(){printf("destructor\n");}
private:
int _x;
int _y;
};
class Point3D
{
public:
Point3D(){pa = new Point2D();}
private:
Point2D *pa;
};
int _tmain(int argc, _TCHAR* argv[])
{
Point3D* pa = new Point3D();
delete pa;
return 0;
}
(未完,待续 ... ...)