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
Why should I employ new instead of truthful old malloc()? A: Constructors/destructors, type safety, overridability. Constructors/destructors: unlike malloc(sizeof(Fred)), new

project on business management

Writing and compiling a program from a given Use Case definition. Follow the Average Temperature in Paradise

I Want a answer for solving the big M method in the topic of simplex method...

pls give me answer 01110-00110 in 2''s comlpiment in detail

Example for Register Storage Class - computer programming? main() { register int i; for (i=0; i { ............... ............... } } /* block exit will free the register

E x p r e s sion and their types: An expression will be in form of mathematical expression with C++ syntax embedded with it. Expressions are of following types which m

Friend for Overloading Operators Sometimes friend functions cannot be avoided. For example with the operator overloading. Consider the following class that have data members to

Given an integer n and a permutation of numbers 1, 2 ... , n-1, n write a program to print the permutation that lexicographically precedes the given input permutation. If the given

Program is to store marks and name, roll no in a file: class stud   {     int rno;     char name[20];     int m1,m2,m3;     public:                 void acce