class Point
{
public:
Point():_x(0),_y(0){}
Point(int x, int y):_x(x),_y(y){}
Point(const Point& rhs)
{
*this = rhs;
}
virtual ~Point(){}
Point& operator=(const Point&);
int X() const {return _x;}
int Y() const {return _y;}
virtual int Z() const =0;
protected:
int _x;
int _y;
};
Point& Point::operator =(const Point &rhs)
{
_x = rhs._x;
_y = rhs._y;
return *this;
}
class Point3d:public virtual Point
{
public:
Point3d():_z(0){}
Point3d(int x, int y, int z):Point(x,y),_z(z){}
Point3d(const Point3d& rhs)//:Point(rhs), _z(rhs._z)
{
//如果如果给Point class定义了构造函数,但是没有明确定义default constructor
//'Point::Point' : no appropriate default constructor available
//因为除非构造在初始化列表里边直接完成,否则肯定是先缺省构造,然后再赋值
*this = rhs;
}
Point3d& operator=(const Point3d&);
virtual int Z() const {return _z;}
protected:
int _z;
};
Point3d& Point3d::operator =(const Point3d &rhs)
{
Point::operator =(rhs);
_z = rhs._z;
return *this;
}
class Vertex:public virtual Point
{
public:
Vertex():next(NULL){}
Vertex(int x, int y):Point(x,y),next(NULL){}
Vertex(const Vertex& rhs):Point(rhs),next(rhs.next){}
~Vertex()
{
if(next)
{
delete next;
next = NULL;
}
}
Vertex& operator=(const Vertex&);
virtual int Z() const {return 0;}
Vertex* Next() const {return next;}
protected:
Vertex *next;
};
Vertex& Vertex::operator=(const Vertex &rhs)
{
Point::operator =(rhs);
next = rhs.next;
return *this;
}
class Vertex3d:public Vertex, public Point3d
{
public:
Vertex3d(){};
Vertex3d(const Vertex& vtx, const Point3d& p3d)
{
//这样根本赋不进去值
//(Vertex)(*this) = vtx;
//(Point3d)(*this) = p3d;
next = vtx.Next();
//虚基类的data有且只有一套
_x = p3d.X();
_y = p3d.Y();
_z = p3d.Z();
}
Vertex3d& operator=(const Vertex3d&);
//如果不做明确定义
// 'Vertex3d' : ambiguous inheritance of 'int Point::Z(void)'
virtual int Z() const
{
return Point3d::Z();
}
private:
int mumble;
};
Vertex3d& Vertex3d::operator=(const Vertex3d& rhs)
{
Vertex::operator =(rhs);
Point3d::operator =(rhs);
mumble = rhs.mumble;
return *this;
}