C++--空类的结构

1.空类,定义时会生成6个成员函数

class Empty
{
};

等价如下:

class Empty
{
  public:
    Empty();                            //缺省构造函数
    Empty(const Empty &rhs);            //拷贝构造函数
    ~Empty();                           //析构函数 
    Empty& operator=(const Empty &rhs); //赋值运算符
    Empty* operator&();                 //取址运算符
    const Empty* operator&() const;     //取址运算符(const版本)
};
//使用时调用情况:
Empty *e = new Empty();    //缺省构造函数
delete e;                  //析构函数
Empty e1;                  //缺省构造函数                               
Empty e2(e1);              //拷贝构造函数
e2 = e1;                   //赋值运算符
Empty *pe1 = &e1;          //取址运算符(非const)
const Empty *pe2 = &e2;    //取址运算符(const)
C++编译器对这些函数的实现:
inline Empty::Empty()                          //缺省构造函数
{
}
inline Empty::~Empty()                         //析构函数
{
}
inline Empty *Empty::operator&()               //取址运算符(非const)
{
  return this; 
}           
inline const Empty *Empty::operator&() const    //取址运算符(const)
{
  return this;
}
inline Empty::Empty(const Empty &rhs)           //拷贝构造函数
{
  //对类的非静态数据成员进行以"成员为单位"逐一拷贝构造
  //固定类型的对象拷贝构造是从源对象到目标对象的"逐位"拷贝
}
 
inline Empty& Empty::operator=(const Empty &rhs) //赋值运算符
{
  //对类的非静态数据成员进行以"成员为单位"逐一赋值
  //固定类型的对象赋值是从源对象到目标对象的"逐位"赋值。
}
posted @ 2023-03-08 13:26  Panor  阅读(64)  评论(0)    收藏  举报