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 | Show 2 more comments1 Answer
Reset to default 3Could 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
版权声明:本文标题:c++ - Could class reference members have in-class initializers? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742048171a2417915.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
double x,y,z,w
that you alias withdouble & 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