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
A: They present a degree of freedom in the interface design options. Member functions & friend functions are equally privileged (100% vested). The major difference is that a fri

Using the Constructor There are basically three ways of creating and initializing the object. The first way to call the constructor is explicitly as :

Write a program in C to implement a FTP client that can interoperate with a default FTP server . The client should implement basic FTP commands GET PUT LIST DELE . GET -> to

Ask question #M i want to undersatnd about tree structures like node leaf etcinimum 100 words accepted#

develop an algorithm using pseudocode for computing cos(x) and sin(x). use a sentinel controlled while loop. use the series definition of e^+-jx

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

A: A using declaration makes it possible to employ a name from a namespace without the scope operator.

Q. Explain that how do we implement two stacks in one array A[1..n] in such a way that neither the stack overflows unless the total number of elements in both stacks together is n.

Bit-wise Operators Some applications require operations to be done on dissimilar bits of a byte separately. Bit-wise operators offer a facility to do just that. There are vario

Destructors A destructor function gets implemented whenever an instance of the class to which it belongs goes out of existence. The primary usage of a destructor function is