admin管理员组

文章数量:1318563

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.

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.

Share Improve this question asked Jan 21 at 10:53 phantomphantom 214 bronze badges 7
  • 2 "as far as I know" And how to do you know this? – HolyBlackCat Commented Jan 21 at 11:17
  • reference members should not have in-class initializers that's a guideline, not a requirement. – j6t Commented Jan 21 at 11:43
  • 1 That means a class with reference members can have a compiler-defined default constructor, but such class is most probably going to cause UB. In other words: don't ever put a reference member in a class, unless you define proper constructor as well as copy constructor, assignment operator and destructor; because such class is not suitable for rule 0, it requires rule 3 or rule 5, besides one extra constructor, and it better have no default constrctor. – Red.Wave Commented Jan 21 at 12:05
  • @Red.Wave: It's incredibly useful to have the GLSL-like vecN struct in C++. You make a double x,y,z,w that you alias with double & u = x, & v = y, & r = x, & g = y, & b = z, & a = w. There's no need for a constructor in that case, so no copy constructor, no assignment operator, no destructor. A well behaved compiler will likely remove the aliasing pointers in that case, since it's just a naming convenience here. – xryl669 Commented Jan 21 at 13:22
  • I asked ChatGPT... for the moment don't ChatGPT really doesn't do C++ very well. – Pepijn Kramer Commented Jan 21 at 16:41
 |  Show 2 more comments

1 Answer 1

Reset to default 3

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.

本文标签: cCould class reference members have inclass initializersStack Overflow