Partager via


Special Member Functions

The topics in this section describe how initialize, copy, move and destroy objects of user-defined class or struct types. in many cases these operations are performed by a class's special member functions—constructors, assignment operators and destructors. These member functions are special because the compiler will automatically generate them if you do not explicitly define them and do not explicitly tell the compiler to not generate them.

In this section

Defaulted and deleted functions

An explicitly-defaulted definition can only be declared for a special member function. When a special member function is explicitly defaulted, the implementation defines it as if it had an implicit definition, except that it may be non-inline (an implicitly-declared special member function is always inline). For more information about defaulted functions, see the "Defaulted and deleted functions" section in Support For C++11 Features (Modern C++).

A deleted definition, also known as a deleted function, is implicitly inline. A program that refers to a deleted function either explicitly or implicitly—other than to declare it—is ill-formed. For more information about deleted functions, see the "Defaulted and deleted functions" section in Support For C++11 Features (Modern C++).

Construction without constructors

It is not always necessary to define a constructor for a class, especially ones that are relatively simple. Users can initialize objects of a class or struct by using uniform initialization, as shown in the following example:

struct TempData
{
    int StationId;
    time_t time;
    double current;
    double max;
    double min;
};


int _tmain(int argc, _TCHAR* argv[])
{
    // Member initialization:
    TempData td { 45978, GetCurrentTime(), 28.9, 37.0, 16.7 };

    // Default initialization = {0,0,0,0,0}
    TempData td_default {};

    //Error C4700 uninitialized local variable
    TempData td_noInit; 
return 0;
}

See Also

Reference

Classes, Structures, and Unions