다음을 통해 공유


Initialization Using Special Member Functions

This section describes initialization using special member functions. It expands on the following discussions of initialization:

  • Initializing Aggregates which describes how to initialize arrays of nonclass types and objects of simple class types. These simple class types cannot have private or protected members, and they cannot have base classes.

  • Constructors, which explains how to initialize class-type objects using special constructor functions.

The default method of initialization is to perform a bit-for-bit copy from the initializer into the object to be initialized. This technique is applicable only to:

  • Objects of built-in types. For example:

    int i = 100;
    
  • Pointers. For example:

    int i;
    int *pi = &i;
    
  • References. For example:

    String sFileName( "FILE.DAT" );
    String &rs = sFileName;
    
  • Objects of class type, where the class has no private or protected members, no virtual functions, and no base classes. For example:

    // spec1_special_member_functions.cpp
    // compile with: /LD
    struct Point
    {
        int x, y;
    };
    
    Point pt = { 10, 20 };   // Static storage class only
    

Classes can specify more refined initialization by defining constructor functions. (For more information about declaring such functions, see Constructors.) If an object is of a class type that has a constructor, the object must be initialized, or there must be a default constructor. Objects that are not specifically initialized invoke the class's default constructor.

See Also

Reference

Special Member Functions (C++)