473,320 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Can You call a Class Destructor Explicitly

hi,
is there any situation that we can call descructor.
Aug 21 '08 #1
3 2784
Ganon11
3,652 Expert 2GB
I'm not sure if this is what you're talking about, but there is a special function in classes called the destructor. It activates when an object of that class goes out of scope. For instance:

Expand|Select|Wrap|Line Numbers
  1. class A {
  2.    private:
  3.       int *a;
  4.       char b;
  5.  
  6.    public:
  7.       A (int aNum, char aChar) { //This is a constructor
  8.          *a = new int;
  9.          *a = aNum;
  10.          b = aChar;
  11.          cout << "End of A's constructor." << endl;
  12.       }
  13.  
  14.       ~A () { // This is the destructor
  15.          cout << "*a = " << *a << endl << "b = " << b << endl << "End of A's destructor." << endl;
  16.          delete a;
  17.       }
  18. };
  19.  
  20. int main() {
  21.    A myAObject(10, 'c');
  22.    return 0;
  23. }
The output is:

Expand|Select|Wrap|Line Numbers
  1. End of A's constructor.
  2. *a = 10
  3. b = c
  4. End of A's destructor.
As you can see, the main() function never calls the destructor. Instead, the destructor is automatically called when the object goes out of scope (in this case, when main() ends).

The destructor is usually used when a class contains a pointer to data that must be deleted when the object is no longer needed (like a in the example class above) - if the pointer is not deleted, then the memory it pointed to is still allocated and takes up space unnecessarily.
Aug 21 '08 #2
weaknessforcats
9,208 Expert Mod 8TB
A destructor is just like any other function. It does not destroy objects. All that happens is that when the compiler has already decided to destroy an object, it will call the destructor so that you can clean up any resource allocations. After that call, the compiler calls the destrcutor on your member variables and after that the object is deleted by the compiler.

Therefore, you can call the destructor anytime you wish to clear out the objects current allocations in the this object. The usual place where you do this is in the class assignment operator overload. Here the contents of the this object have to be cleaned up before they are overwritten by the contents of the object on the right of the assignment operator, the RVAL.

The compiler first calls the assignment operator on the class member variables and then calls the assignment operator for the this object. Each of these assignment operator calls has the following general flow:
Expand|Select|Wrap|Line Numbers
  1. MyClass MyClass::operator=(const MyClass& rval)
  2. {
  3.     if (this == &rval) return;         //do not assign to yourself
  4.     this->~MyClass();                //call destructor to clean ourselves up
  5.  
  6.     //do the reallocation here to accommodate the rval members
  7.  
  8.     return *this;
  9. }
  10.  
You could, of course have a private member function call by both the class destructor and by the assignment operator to do this cleanup but often just calling the destructor makes the most sense.
Aug 21 '08 #3
A destructor is just like any other function. It does not destroy objects. All that happens is that when the compiler has already decided to destroy an object, it will call the destructor so that you can clean up any resource allocations. After that call, the compiler calls the destrcutor on your member variables and after that the object is deleted by the compiler.

Therefore, you can call the destructor anytime you wish to clear out the objects current allocations in the this object. The usual place where you do this is in the class assignment operator overload. Here the contents of the this object have to be cleaned up before they are overwritten by the contents of the object on the right of the assignment operator, the RVAL.

The compiler first calls the assignment operator on the class member variables and then calls the assignment operator for the this object. Each of these assignment operator calls has the following general flow:
Expand|Select|Wrap|Line Numbers
  1. MyClass MyClass::operator=(const MyClass& rval)
  2. {
  3.     if (this == &rval) return;         //do not assign to yourself
  4.     this->~MyClass();                //call destructor to clean ourselves up
  5.  
  6.     //do the reallocation here to accommodate the rval members
  7.  
  8.     return *this;
  9. }
  10.  
You could, of course have a private member function call by both the class destructor and by the assignment operator to do this cleanup but often just calling the destructor makes the most sense.

thank u dude its really a good post....
Aug 22 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: tomek | last post by:
Can anybody explain why can I not get rid of the above message in the following class definition? Thank you in advance. class BasicMidReader : public TMemoryStream { //some private date...
23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
8
by: trying_to_learn | last post by:
Why do we need to explicitly call the copy constructor and the operator = , for base class and member objects in composition? ....book says "You must explicitly call the GameBoard copy-constructor...
9
by: Daniel Kay | last post by:
Hello! I have written two template classes which implement the observerpattern in C++. I hope I manage to desribe the problem I have. template<class T> class Observer { /* ... */ }; ...
5
by: Frederick Gotham | last post by:
If we have a simple class such as follows: #include <string> struct MyStruct { std::string member; MyStruct(unsigned const i) {
13
by: shsingh | last post by:
I have a class A containing some map as data variables. I creat an object of class A on heap by allocatiing memory by using "malloc". This will return me the required memory but the object is not...
3
by: mike.arsenault | last post by:
Hello I need some help from anyone that can provide it. Below is a function inside a template collection class that I'm writing. I have a TYPE * that points to an allocated memory location, and...
2
by: bg_ie | last post by:
Hi, I'm creating objects in my python script belonging to a COM object which I dispatch using win32com.client.DispatchEx. Hence, dllhost.dll is the concerned process. The problem is that the...
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
7
by: Rahul | last post by:
Hi Everyone, I was trying to implement a final class and i start having the destructor of the final class as private, class A { ~A() { printf("destructor invoked\n");
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.