Can i explicitly call a destructor if i''ve allocated my , C/C++ Programming

Can I explicitly call a destructor if I've allocated my object with new?

Posted Date: 3/15/2013 5:35:42 AM | Location : United States





A: perhaps not.        

 Unless you utilized placement new, you must simply delete the object instead of explicitly calling the destructor. For instance, suppose you allocated the object through a typical new expression:

Fred* p = new Fred();

Then the destructor Fred::~Fred() will auto magically get called while you delete it via:

delete p; // Automagically calls p->~Fred()

You must not explicitly call the destructor, as doing so won''t release the memory which was allocated for the Fred object itself. Remember: delete p does two things: first it calls the destructor and second it deallocates the memory.

 

Posted by | Posted Date: 3/15/2013 5:36:19 AM


Related Discussions:- Can i explicitly call a destructor if i''ve allocated my , Assignment Help, Ask Question on Can i explicitly call a destructor if i''ve allocated my , Get Answer, Expert's Help, Can i explicitly call a destructor if i''ve allocated my Discussions

Write discussion on Can i explicitly call a destructor if i''ve allocated my
Your posts are moderated
Related Questions
Define a structure to store roll no, name and marks of a student. b) Using the structure of Q2. a), above write a ''C'' program to create a file "student.dat". There must be one r

People sometimes give their phone numbers using one or more alphabetic characters. Create a program that converts the alphabetic characters to their corresponding numbers. For exam

Define Storage Classes of c program - computer programming? Each variable and function in C language has two attributes that are type and storage class. If storage class of a v

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

flowchart of c programing to check the given number is prime or not

Illustration of Grid function: For illustration, the script below creates two individual figure windows. At First, it clears the figure window. Then, it generates an x vector

A: auto: the default. Variables are created and initialized automatically while they are defined and destroyed at the ending of the block containing their definition. They are not

To write a program to use different levels of array of pointer to pointer & display the output as 1 1 1 2 2 2 3 3 3

What if one forget the [] when deleteing array allocated through new T[n]? A: All life comes to a disastrous end. It is the programmer's responsibility not the compiler's to