Jump to content

Special member functions

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by 77.2.182.85 (talk) at 16:45, 16 February 2015 (The given destructor is not equivalent to the implicit destructor and would in fact destroy member variables twice.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Special member functions[1] in C++ are functions which the compiler will automatically generate if they are used, but not declared explicitly by the programmer. The special member functions are:

If a destructor is declared generation of a copy constructor is deprecated.
  • Move constructor if no copy constructor, move assignment operator or destructor is explicitly declared.
  • Copy assignment operator if no move constructor or move assignment operator is explicitly declared.
If a destructor is declared generation of a copy assignment operator is deprecated.

In these cases the compiler generated versions of these functions perform a memberwise operation. For example the compiler generated destructor will destroy each sub-object (base class or member) of the object.

The compiler generated functions will be public, non-virtual[2] and the copy constructor and assignment operators will receive const& parameters (and not be of the alternative legal forms).

Example

The following example depicts two classes: Explicit for which all C++98 special member functions are explicitly declared and Implicit for which none are declared.

#include <iostream>

class Explicit {
    friend class Implicit;
    string msg;
public:
    Explicit() : msg("") 
    {
        std::cout << "Default constructor " << msg << '\n';
    }

    Explicit(const string& value) : msg(value) 
    {
        std::cout << "Non-default constructor " << msg << '\n';
    }

    Explicit(const Explicit& other) : msg(other.msg) 
    {
        std::cout << "Copy constructor " << msg << '\n';
    }

     Explicit& operator=(const Explicit& other) 
    {
        std::cout << "Copy assignment operator " << msg << '\n';
        if (this != &other) {
            msg = other.msg;
        }
        return *this;
    }
    
    ~Explicit() 
    {
        std::cout << "Destructor " << msg << '\n';
    }
};

class Implicit : public Explicit {
    int i;
    void* p;
    Explicit member;
public:
    void Spew() 
    { 
        std::cout << "Implicit(" << msg << ", " << member.msg << ")\n"; 
    }
};

Signatures

Here are the correct signatures of each special member function

class MyClass {
    MyClass();                                // Default ctor
    MyClass(int a_param);                     // Ctor
    MyClass(const MyClass& other);            // Copy ctor
    MyClass(MyClass&& other);                 // Move ctor
    MyClass& operator=(const MyClass& other); // Copy assignment op
    MyClass& operator=(MyClass&& other);      // Move assignment op
    ~MyClass();                               // Dtor
};

C++98

In C++98 before the introduction of move semantics the special member functions[3] were:

References

  1. ^ ISO/IEC (2011). ISO/IEC 14882:2011 (3 ed.). ISO/IEC. pp. §12.
  2. ^ Except for the destructor if a base class already has a virtual destructor.
  3. ^ ISO/IEC (1998). International Standard ISO/IEC 14882: Programming languages—C++ = Languages de programmation—C++ (1 ed.). ISO/IEC. pp. §12. OCLC 71718919.