Special member functions

Special member functions

Special member functions [cite book
last = ISO/IEC
authorlink = International Organization for Standardization
title = International Standard ISO/IEC 14882: Programming languages—C++ = Languages de programmation—C++
edition = 1
publisher = ISO/IEC
date = 1998
oclc = 71718919
pages = §12
] 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:
* Default constructor (if no other constructor is explicitly declared)
* Copy constructor
* Copy assignment operator
* Destructor

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.

Example

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

class Explicit { friend class Implicit; string msg; public: Explicit(void) : msg("") { cout << "Default constructor " << msg << endl; } Explicit(const string& value) : msg(value) { cout << "Non-default constructor " << msg << endl; } Explicit(const Explicit& other) : msg(other.msg) { cout << "Copy constructor " << msg << endl; } Explicit& operator=(const Explicit& other) { cout << "Copy assignment operator " << msg << endl; if (this != &other) { msg = other.msg; } return *this; } ~Explicit(void) { cout << "Destructor " << msg << endl; ;

class Implicit : public Explicit { int i; void* p; Explicit member; public: void Spew(void) { cout << "Implicit(" << msg << ", " << member.msg << ")" << 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 constructionImplicit::~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


Wikimedia Foundation. 2010.

Игры ⚽ Поможем написать реферат

Look at other dictionaries:

  • Member state of the European Union — Member states of the European Union …   Wikipedia

  • Special Tasks and Rescue — (STAR Force) is the Police Tactical Group of the South Australia Police.HistoryFormed on 30 November, 1978 the South Australian Police STAR Force was a rationalisation of specialist resources into one command/unit. Specialist units had existed… …   Wikipedia

  • Special Organization (Ottoman Empire) — Special Organization was the name given to a three member executive committee of Ministry of the Interior established by the Committee of Union and Progress of the Ottoman Empire. It is speculated that the organization was planned and created… …   Wikipedia

  • Special Activities Division — The Special Activities Division (SAD) [cite news first =Bob last =Woodward authorlink = coauthors = year =2001 month =November 18 title =Secret CIA Units Playing a Central Combat Role newspaper =Washington Post pages =A01 url… …   Wikipedia

  • Special Police Dekaranger — Doggie Kruger redirects here. For the similarly named character in Power Rangers: S.P.D., see Anubis Cruger. Tokusou Sentai Dekaranger (from left to right: Deka Master, Deka Blue, Deka Yellow, Deka Break, Deka Red, Deka Pink, Deka Green and Deka… …   Wikipedia

  • Special Executive — Infobox comics organization name=The Special Executive imagesize= caption= publisher=Marvel Comics debut= Dr. Who Monthly #51 (April, 1981) Daredevils #5 (May, 1983) creators=Alan Moore (writer) David Lloyd (artist) Alan Davis (writer/artist)… …   Wikipedia

  • Special Forces (Maldives) — MNDF Special Forces Special Forces Badge Active 2008 Present Country …   Wikipedia

  • Special constable — A special (police) constable (SC or SPC) is a law enforcement officer who is not a regular member of a police force, but is a member of a volunteer police auxiliary. Many police departments are complemented by a Special Constabulary, members of… …   Wikipedia

  • Special Forces Association — The Special Forces Association (SFA) is a non profit fraternal organization for current and retired U.S. Army Special Forces soldiers, also known as Green Berets. Established in 1964, the association is based at Fort Bragg, North Carolina, with… …   Wikipedia

  • Member of Parliament — Legislature This series is part of the Politics series Legislature Legislatures by country Parliament Member of Parliament Parliamentary group Parliamentary group leader Congress Congressperson …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”