📄 12aggreg.html
字号:
<html>
<head>
<title>Initialization of Aggregates</title>
<meta name="description" content="Explicit initialization of arrays and structures in C++">
<meta name="keywords" content="aggregate, data structure, initialization, literal">
<link rel="stylesheet" href="rs.css" tppabs="http://www.relisoft.com/book/rs.css">
</head>
<body background="margin.gif" tppabs="http://www.relisoft.com/book/images/margin.gif" bgcolor="#FFFFDC">
<!-- Main Table -->
<table cellpadding="6">
<tr>
<td width="78">
<td>
<h3>Initialization of Aggregates</h3>
<p class=topics>Explicit initialization of classes and arrays.
<p>Just as you can explicitly initialize an array of characters using a literal string
<!-- Code -->
<table width="100%" cellspacing=10><tr>
<td class=codeTable>
<pre>char string [] = "Literal String";</pre>
</table>
<!-- End Code -->
you can initialize other aggregate data structures--classes and arrays. An object of a given class can be explicitly initialized if and only if all its non-static data members are public and there is no base class, no virtual functions and no user-defined constructor. All public data members must be explicitly initializable as well (a little recursion here). For instance, if you have
<!-- Code -->
<table width="100%" cellspacing=10><tr>
<td class=codeTable>
<pre>class Initializable
{
public:
// no constructor
int _val;
char const * _string;
Foo * _pFoo;
};</pre>
</table>
<!-- End Code -->
you can define an instance of such class and initialize all its members at once
<!-- Code -->
<table width="100%" cellspacing=10><tr>
<td class=codeTable>
<pre>Foo foo;
Initializable init = { 1, "Literal String", &foo };</pre>
</table>
<!-- End Code -->
<p>Since <var>Initializable</var> is initializable, you can use it as a data member of another initializable class.
<!-- Code -->
<table width="100%" cellspacing=10><tr>
<td class=codeTable>
<pre>class BigInitializable
{
public:
Initializable _init;
double _pi;
};
BigInitializable big = { { 1, "Literal String", &foo }, 3.14 };</pre>
</table>
<!-- End Code -->
<p>As you see, you can nest initializations.
<p>You can also explicitly initialize an array of objects. They may be of a simple or aggregate type. They may even be arrays of arrays of objects. Here are a few examples.
<!-- Code -->
<table width="100%" cellspacing=10><tr>
<td class=codeTable>
<pre>char string [] = { 'A', 'B', 'C', '\0' };</pre>
</table>
<!-- End Code -->
is equivalent to its shorthand
<!-- Code -->
<table width="100%" cellspacing=10><tr>
<td class=codeTable>
<pre>char string [] = "ABC";</pre>
</table>
<!-- End Code -->
<p>Here's another example
<!-- Code -->
<table width="100%" cellspacing=10><tr>
<td class=codeTable>
<pre>Initializable init [2] = {
{ 1, "Literal String", &foo1 },
{ 2, "Another String", &foo2 } };</pre>
</table>
<!-- End Code -->
<p>We used this method in the initialization of our array of <var>FuncitionEntry</var> objects.
<p>If objects in the array have single-argument constructors, you can specify these arguments in the initializer list. For instance,
<!-- Code --><table width=100% cellspacing=10><tr> <td class=codetable>
<pre>CelestialBody solarSystem = { 0.33, 4.87, 5.98, 0.64, 1900, 569, 87, 103, 0.66 };</pre>
</td></tr></table><!-- End Code -->
<p class="continue">where masses of planets are given in units of 10<sup>24</sup>kg.
<br><a href="13exerc.html" tppabs="http://www.relisoft.com/book/lang/project/13exerc.html">Next.</a>
</table>
<!-- End Main Table -->
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -