Writing a Default Constructor Forces Zero-Initialization?

前端 未结 3 499
有刺的猬
有刺的猬 2021-01-06 23:23

These are my class definitions:

class Foo{
    int _ent;
public:
    void printEnt() const{cout << _ent << \' \';}
};

class Bar{
    Foo _foo;
p         


        
3条回答
  •  天命终不由人
    2021-01-06 23:52

    When you provided a default constructor you no longer get the compiler generated one. Since your default constructor initializes the member to 0 the member will always be 0, this is especially true since the member is private and you have no way to change it.

提交回复
热议问题