编程语言
首页 > 编程语言> > c++对象模型 拷贝、构造、虚构

c++对象模型 拷贝、构造、虚构

作者:互联网

class Abstract_base { public: virtual ~Abstract_base() = 0; virtual void interface() const = 0; virtual const char* mumble() const { return _mumble; } protected: char* _mumble; }

​ 以上抽象基类声明有几个问题:

  1. 即使class被声明为抽象基类,其依然需要explicit constructor来初始化protected data member _mumble,否则derived class无法决定_mumble初值
  2. 抽象基类的virtual destructor不要声明为pure。因为每个derived class destructor会被编译器扩张,以静态方式调用每个virtual base class和上一层base class的destructor
 
  1. mumble()不应声明为virtual function,因为其定义的内容和类型无关,derived class并不会改写此函数//合理的声明 class Abstract_base { public: virtual ~Abstract_base(); virtual void interface() = 0; const char* mumble() const { return _mumble; } protected: Abstract_base( char* pc = 0 ); char* _mumble; }

2|0对象构造

2|1不继承

typedef struct { float x, y, z; }Point; Point global; Point foobar() { Point local; Point* heap = new Point; *heap = local; delete heap; return local; } class Point { public: Point( float x = 0.0, float y = 0.0, float z = 0.0 ) : _x(x), _y(y), _z(z) { } //没有copy constructor,copy operator,destructor private: float _x, _y, _z; } void do() { Point local1 = {1.0, 1.0, 1.0}; } class Point { public: Point( float x = 0.0, float y = 0.0, float z = 0.0 ) : _x(x), _y(y), _z(z) { } virtual float z(); //没有copy constructor,copy operator,destructor private: float _x, _y; }

标签:java,函数,学习,系统,语言,平台,方法,安装,QML,c++,数据
来源: