美文网首页
What Compile Do When An Empty Cl

What Compile Do When An Empty Cl

作者: BeeCaffe | 来源:发表于2020-04-29 15:37 被阅读0次

    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(){};
    };
    

    相关文章

      网友评论

          本文标题:What Compile Do When An Empty Cl

          本文链接:https://www.haomeiwen.com/subject/owtnwhtx.html