C++中的聚合类

聚合类是一种没有用户定义的构造函数,没有私有(private)和保护(protected)非静态数据成员,没有基类,没有虚函数。这样的类可以由封闭的大括号用逗号分隔开初始化列表。下列的代码在 C 和 C++ 具有相同的语法:

<span class="kw4" style="color: rgb(0, 0, 255); ">struct</span> C
<span class="br0" style="color: rgb(0, 128, 0); ">{</span>
  <span class="kw4" style="color: rgb(0, 0, 255); ">int</span> a<span class="sy4" style="color: rgb(0, 128, 128); ">;</span>
  <span class="kw4" style="color: rgb(0, 0, 255); ">double</span> b<span class="sy4" style="color: rgb(0, 128, 128); ">;</span>
<span class="br0" style="color: rgb(0, 128, 0); ">}</span><span class="sy4" style="color: rgb(0, 128, 128); ">;</span>
 
<span class="kw4" style="color: rgb(0, 0, 255); ">struct</span> D
<span class="br0" style="color: rgb(0, 128, 0); ">{</span>
  <span class="kw4" style="color: rgb(0, 0, 255); ">int</span> a<span class="sy4" style="color: rgb(0, 128, 128); ">;</span> 
  <span class="kw4" style="color: rgb(0, 0, 255); ">double</span> b<span class="sy4" style="color: rgb(0, 128, 128); ">;</span>
  C c<span class="sy4" style="color: rgb(0, 128, 128); ">;</span>
<span class="br0" style="color: rgb(0, 128, 0); ">}</span><span class="sy4" style="color: rgb(0, 128, 128); ">;</span>
 
<span class="co1" style="color: rgb(102, 102, 102); ">// initialize an object of type C with an initializer-list</span>
C c <span class="sy1" style="color: rgb(0, 0, 128); ">=</span> <span class="br0" style="color: rgb(0, 128, 0); ">{</span> <span class="nu0" style="color: rgb(0, 0, 221); ">1</span>, <span class="nu0" style="color: rgb(0, 0, 221); ">2</span> <span class="br0" style="color: rgb(0, 128, 0); ">}</span><span class="sy4" style="color: rgb(0, 128, 128); ">;</span>
 
<span class="co1" style="color: rgb(102, 102, 102); ">// D has a sub-aggregate of type C. In such cases initializer-clauses can be nested</span>
D d <span class="sy1" style="color: rgb(0, 0, 128); ">=</span> <span class="br0" style="color: rgb(0, 128, 0); ">{</span> <span class="nu0" style="color: rgb(0, 0, 221); ">10</span>, <span class="nu0" style="color: rgb(0, 0, 221); ">20</span>, <span class="br0" style="color: rgb(0, 128, 0); ">{</span> <span class="nu0" style="color: rgb(0, 0, 221); ">1</span>, <span class="nu0" style="color: rgb(0, 0, 221); ">2</span> <span class="br0" style="color: rgb(0, 128, 0); ">}</span> <span class="br0" style="color: rgb(0, 128, 0); ">}</span><span class="sy4" style="color: rgb(0, 128, 128); ">;</span>
<span class="sy4" style="color: rgb(0, 128, 128); ">
</span>
<span class="sy4" style="color: rgb(0, 128, 128); "></span><pre class="de1" name="code" style="white-space: pre-wrap; word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; font-size: 1em; line-height: 1.2em; border: 0px none white; padding: 0px; vertical-align: top; "><span class="sy4" style="color: rgb(0, 128, 128); "></span><p style="margin-top: 5px; margin-bottom: 5px; color: rgb(68, 68, 68); line-height: 22px; font-size: 13px; font-family: Simsun; ">如果一个类里面包含了用户自定义的构造函数,而又用{ xx, xx, ...}来初始化它的对象,编译器就会报错</p><p style="margin-top: 5px; margin-bottom: 5px; color: rgb(68, 68, 68); line-height: 22px; font-size: 13px; font-family: Simsun; ">vc —— error C2552: "xx" 不能用初始值设定项列表初始化非聚合</p><p style="margin-top: 5px; margin-bottom: 5px; color: rgb(68, 68, 68); line-height: 22px; font-size: 13px; font-family: Simsun; ">gcc—— error: xx must be initialized by constructor, not by '{...}'</p>

 
  
<span class="sy4" style="color: rgb(0, 128, 128); ">
</span>
<span class="sy4" style="color: rgb(0, 128, 128); "></span><p style="color: rgb(68, 68, 68); line-height: 22px; margin-top: 5px; margin-bottom: 5px; font-size: 13px; padding-top: 1em; font-family: Simsun; "><a target=_blank href="http://one.rdaili.com/baidu.com.php?u=RBB%2BGzHpXLSeTpmJO7BNWtb2xAu7n4pJBnb8plPMluTzLWw6yaFoEk1tUew3C7delcBJp9kuC2GRvQ%3D%3D&b=3" style="color: rgb(239, 123, 10); text-decoration: initial; ">聚合</a>定义为:</p><ul style="font-size: 13px; font-family: Simsun; "><li><p style="color: rgb(68, 68, 68); line-height: 20px; margin-top: 5px; margin-bottom: 5px; font-size: 12px; ">数组</p></li><li><p style="color: rgb(68, 68, 68); line-height: 20px; margin-top: 5px; margin-bottom: 5px; font-size: 12px; ">没有以下内容的类、结构和联合:</p><ul style="font-size: 12px; "><li><p style="color: rgb(68, 68, 68); line-height: 20px; margin-top: 5px; margin-bottom: 5px; ">构造函数</p></li><li><p style="color: rgb(68, 68, 68); line-height: 20px; margin-top: 5px; margin-bottom: 5px; ">私有或受保护的成员</p></li><li><p style="color: rgb(68, 68, 68); line-height: 20px; margin-top: 5px; margin-bottom: 5px; ">基类</p></li><li><p style="color: rgb(68, 68, 68); line-height: 20px; margin-top: 5px; margin-bottom: 5px; ">虚函数</p></li></ul></li></ul><p style="color: rgb(68, 68, 68); line-height: 22px; margin-top: 5px; margin-bottom: 5px; font-size: 13px; font-family: Simsun; ">编译器不允许在包含构造函数的聚合中使用数据类型。</p>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值