What Compile Do When An Empty Class Is Declared?

What Compile Do When An Empty Class Is Declared?

  • conclusion: when an empty class is declared, the compile will generate some members by default.
    (1) default constructor;
    (2) copy constructor;
    (3) operator=();
    (4) default destructor;
  • code
// the mepty class
class Empty{
};

//euqal to following
class Empty{
public:
    Empty(){};
    Empty(const Empty&){};
    Empty& operator=(const Empty&){return *this};
    inline ~Empty(){};
};

if user declares a constructor, the compile will not generate default constructor

  • explaination: when a constrcutor is decalred in C++, the compile will not generate default constructor any more. Due to compile regards you do not need a default constructor anymore.
  • code
// the mepty class
class Empty{
 public:
    Empty(int val):m_data(val){}
  private:
    int m_data;
};

//equals to following
class Empty{
public:
    Empty(int val):m_data(val){}
    //Empty(){};
    Empty(const Empty&){};
    Empty& operator=(const Empty&){return *this};
    inline ~Empty(){};
};
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc阅读 2,938评论 0 0
  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom阅读 2,731评论 0 3
  • 英文文档,一开始我也是抗拒的,边翻译边看,也就花费了1个小时基本就阅读过了,我的英文基础其实很差。附上链接:链接:...
    lonecolonel阅读 10,036评论 3 1
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,537评论 0 13
  • 两个人在一起是想要快乐的,所以平时尽量不要带有负能量,但人也不是完美的,怎么可能没有负能量呢?所以我们平时要分出1...
    叶子游戏推荐阅读 94评论 0 0