c++c++11

Could class reference members have in-class initializers?


I am reading C++ Primer, and in section 13.1.6 it states:

The synthesized default constructor is defined as deleted if the class has a member with a deleted or inaccessible destructor; or has a reference member that does not have an in-class initializer (§ 2.6.1, p. 73); or has a const member whose type does not explicitly define a default constructor and that member does not have an in-class initializer.

But as far as I know, reference members should not have in-class initializers. Reference members of a class should be initialized through a constructor that takes parameters. This text makes me feel confused.Can I understand it as "or has a reference member"?

I asked ChatGPT and also searched for in-class initializer, but both support the conclusion that reference member should not have in-class initializers.


Solution

  • Could class reference members have in-class initializers?

    Yes, I think so.

    Like this:

    struct A
    {
       int a;
       int & b = a;
    };
    

    Since the reference is completely contained within the declaration, it can be initialized in class.