Share via


Explicit Initialization

C++ supports two forms of explicit initialization.

  • 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";
    

    Although 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:

  • Creating a temporary object of the same type as the object being initialized.

  • Copying the temporary object to the object.

The constructor must be accessible before the compiler can perform 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 (under /Za, /Ze (Disable Language Extensions)). Consider the following example:

// spec1_explicit_initialization.cpp
// compile with: /Za
class anInt {
   anInt( const anInt &copy ) {}   // private copy constructor

public:
   anInt( int ) {}   // public constructor
};

int main() {
   // Access-control violation. 
   // Attempt to reference private copy constructor.
   anInt myInt = 7;   // C2248

   anInt myInt2(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, ... initializer)

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.)

See Also

Reference

Initialization Using Special Member Functions