Explicit Initialization

来源:互联网 发布:vba 数组声明 编辑:程序博客网 时间:2024/06/07 08:19

C++ supports different forms of explicit initialization.

  • In The C++ Standard Library page 14, it produced “explicit initialization”which is useful for template
    template <class T>
    void f( )
    {
    T x = T( );
    ...
    }

    x will be initialized as 0

    also:   int a = int( );  a will be initialized as 0

  • 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: /Zaclass anInt {   anInt( const anInt &copy ) {}   // private copy constructorpublic:   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++" );
0 0