UsingMultiple Initializers
When we initialize an object of a built-in type, there is only one way to do so: We supply a value, and that value is copied into the newly defined object. For built-in types, there is little difference between the direct and the copy forms of initialization.
For objects of a class type, there are initializations that can be done only using direct-initialization. To understand why, we need to know a bit about how classes control initialization.
Each class may define one or more special member functions (Section 1.5.2, p. 24) that say how we can initialize variables of the class type. The member functions that define how initialization works are known as constructors. Like any function, a constructor can take multiple arguments. A class may define several constructors, each of which must take a different number or type of arguments.
As an example, we’ll look a bit at the string class, which we’ll cover in more detail in Chapter 3. The string type is defined by the library and holds character strings of varying sizes. To use strings, we must include the string header. Like the IO types, string is defined in the std namespace.
The string class defines several constructors, giving us various ways to initialize a string. One way we can initialize a string is as a copy of a character string literal:
- #include <string>
-
std::string titleA = "C++(www.cppentry.com) Primer, 4th Ed."; std::string titleB("C++(www.cppentry.com) Primer, 4th Ed.");
In this case, either initialization form can be used. Both definitions create a string
object whose initial value is a copy of the specified string literal.
However, we can also initialize a string from a count and a character. Doing so creates a string containing the specified character repeated as many times as indicated by the count:
- std::string all_nines(10, ’9’);
In this case, the only way to initialize all_nines is by using the direct form of initialization. It is not possible to use copy-initialization with multiple initializers.
如果构造函数有不止一个实参,那么只能直接初始化。
InitializingMultiple Variables
建议一行代码、一条语句只定义一个变量。
When a definition defines two or more variables, each variable may have its own initializer. The name of an object becomes visible immediately, and so it is possible to initialize a subsequent variable to the value of one defined earlier in the
same definition. Initialized and uninitialized variablesmay be defined in the same definition. Both forms of initialization syntax may be intermixed:
- #include <string>
-
- double salary = 9999.99,
- wage(salary + 0.01);
-
- int interval,
- month = 8, day = 7, year = 1955;
-
- std::string title("C++(www.cppentry.com) Primer, 4th Ed."),
- publisher = "A-W";
An object can be initialized with an arbitrarily complex expression, including the return value of a function:
- double price = 109.99, discount = 0.16;
- double sale_price = apply_discount(price, discount);
In this example, apply_discount is a function that takes two values of type double and returns a value of type double. We pass the variables price and discount to that function and use its return value to initialize sale_price.