Explicit Initialization

Two forms of explicit initialization are supported in C++:

Supplying an initializer list in parentheses:

String sFileName( "FILE.DAT" );

The items in the parenthesized list are considered arguments to the class constructor. This form of initialization enables initialization of an object with more than one value and can also be used in conjunction with the new operator. For example:

Rect *pRect = new Rect( 10, 15, 24, 97 );

Supplying a single initializer using the equal-sign initialization syntax. For example:

String sFileName = "FILE.DAT";

While the preceding example works the same way as the example shown for String in the first list item, the syntax is not adaptable to use with objects allocated on the free store.

The single expression on the right of the equal sign is taken as the argument to the class's copy constructor; therefore, it must be a type that can be converted to the class type.

Note that because the equal sign (=) in the context of initialization is different from an assignment operator, overloading operator= has no effect on
initialization.

The equal-sign initialization syntax is different from the function-style syntax, even though the generated code is identical in most cases. The difference is that when the equal-sign syntax is used, the compiler has to behave as if the following sequence of events were taking place:

Create a temporary object of the same type as the object being initialized.

Copy the temporary object to the object.

The compiler must perform accessibility checking on the copy constructor before performing these steps. Even though the compiler can eliminate the temporary creation and copy steps in most cases, an inaccessible copy constructor causes equal-sign initialization to fail. Consider the following example:

class anInt

{

anInt( const anInt& ); // Private copy constructor.

public:

anInt( int ); // Public int constructor.

};

...

anInt myInt = 7; // Access-control violation. Attempt to

// reference private copy constructor.

anInt myInt( 7 ); // Correct; no copy constructor called.

When a function is called, class-type arguments passed by value and objects returned by value are conceptually initialized using the form:

type-name name = value

For example:

String s = "C++";

Therefore, it follows that the argument type must be a type that can be converted to the class type being passed as an argument. The class's copy constructor, as well as user-defined conversion operators or constructors that accept the type of the actual argument, must be public.

In expressions that use the new operator, the objects allocated on the free store are conceptually initialized using the form:

type-name name( initializer1, initializer2, ... initializern )

For example:

String *ps = new String( "C++" );

Initializers for base-class components, and member objects of a class are also conceptually initialized this way. (For more information, see “Initializing Bases and Members”.)