C++中的聚合类

来源:互联网 发布:cad制图软件学习 编辑:程序博客网 时间:2024/05/18 03:49

聚合类是一种没有用户定义的构造函数,没有私有(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