When i develop a destructor, do i require to explicitly call, C/C++ Programming

When I develop a destructor, do I require to explicitly call the destructors for my member objects?

 

 

Posted Date: 3/15/2013 5:38:05 AM | Location : United States





A: No. You never have to explicitly call a destructor (except with placement new).

A class''s destructor (whether or not you explicitly describe one) automatically invokes the destructors for member objects. They are destroyed in reverse order they show in the declaration for the class.

class Member {               

public:

~Member();

...

};

class Fred {

public:

~Fred();

... private: Member x_; Member y_; Member z_;

};

Fred::~Fred()

{

// Compiler automagically calls z_.~Member()

// Compiler automagically calls y_.~Member()

// Compiler automagically calls x_.~Member()

}

 

Posted by | Posted Date: 3/15/2013 5:38:38 AM

A: No. You never require to explicitly call a destructor (except with placement new).

A derived class''s destructor (whether or not you explicitly define one) automagically invokes the destructors for base class sub objects. Base classes are destructed after member objects. In the event of multiple inheritances, direct base classes are destructed in the reverse order of their appearance in the inheritance list.

class Member {

public:

~Member();

...

};

class Base {

public:

virtual ~Base(); // A virtual destructor

...

};

class Derived : public Base {

public:

~Derived();

... private: Member x_;

};

Derived::~Derived()

{

// Compiler automagically calls x_.~Member()

// Compiler automagically calls Base::~Base()

}

Note: Order dependencies along with virtual inheritance are trickier. If you are relying onto order dependencies within a virtual inheritance hierarchy, you''ll require many more information than is in this

 

Posted by | Posted Date: 3/15/2013 5:39:30 AM


Related Discussions:- When i develop a destructor, do i require to explicitly call, Assignment Help, Ask Question on When i develop a destructor, do i require to explicitly call, Get Answer, Expert's Help, When i develop a destructor, do i require to explicitly call Discussions

Write discussion on When i develop a destructor, do i require to explicitly call
Your posts are moderated
Related Questions
At a shop of marbles, packs of marbles are prepared. Packets are named A, B, C, D, E …….. All packets are kept in a VERTICAL SHELF in random order. Any numbers of packets with thes

A palindrome is a string that reads the same from both the ends. Given a string S convert it to a palindrome by doing character replacement. Your task is to convert S to palindrome

search words from a passage and return snippets in html format..

program to check whether a given point lies inside a rectangle or not

What are header files? What are their uses? - Header files are also known as library files. - They carry two significant things: definitions and prototypes of functions bein

// Basic pointer code #include "stdafx.h" #include iostream using namespace std; int _tmain(int argc, _TCHAR* argv[]) {             int FirstNumber, SecondNumber;

Dynamic Initialization of objects: It is initializing the objects by passing the valued to the constructor from the user input or other means.   Through cin operator a value

Write a program that predicts users’ age (0-128 years old) with at most 7 questions. The game starts with asking the user whether he/she is younger or older than G (an initial gues


in mobiles mumbers to alphabets