Jump to content

Special member functions

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Lanzkron (talk | contribs) at 07:43, 17 August 2011 (Undid revision 445157924 by Demonkoryu (talk) Please don't turn this into an edit war, we've discussed this in Wikipedia_talk:WikiProject_C++#Coding_Style_Conventions). 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:

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

Note: In C++0x with the introduction of move semantics, the move constructor and move assignment operator are added to the list of special member functions with the following rules.
  • Both copy constructor and move constructor are defined by the compiler unless one of them was explicitly defined
  • Both flavours of assignment operators are defined by the compiler unless one of them was explicitly defined


Example

The following example depicts two classes: Explicit for which all 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 << std::endl;
    }

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

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

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

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

In this case the class Implicit has not explicitly defined the destructor and the compiler will create a destructor equivalently to this:

// Sub-objects are destroyed in the opposite order to their construction
Implicit::~Implicit() {
    member.~Explicit(); // destroy member
    (void)p;           // do nothing for p, void* has no destructor
    (void)i;           // do nothing for i, int has no destructor
    ~Explicit();        // call the base class's destructor
}

References

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