declaring a const instance of a class

后端 未结 4 804
面向向阳花
面向向阳花 2020-12-08 19:11

Let\'s say I have a class defined as follows:

class foo{};

now, this is perfectly acceptable;

foo f;

how

4条回答
  •  再見小時候
    2020-12-08 19:50

    I think there are more alternatives. You can do

    const foo f={};
    

    or

    const foo f(());
    

    Const means you can not assign to it later, so you have to initialize it. Since you did not define default constructor the compiler assumes it has to be initialized by you. std::string has default constructor so it is called implicitly by compiler.

提交回复
热议问题