3

I have been asked a question as, a class has multiple constructors but why it has only one destructor?

I gave below example,

class abc
{
public:
    int a;
    abc()
    {
        cout << "Default\n";
    }
    abc(int)
    {
        cout << "Int\n";
    }
    ~abc()
    {
        cout << "Destructor\n";
    }
};
int main()
{
    abc ab;
    abc a(5);
}

And I explained as before abc a(5); gets called destructor will get called so, there will only one object at a particular point of time. I ran the above code now in my PC but it gave me output as

Default
Int
Destructor
Destructor

If this is so then why do we haveone destructor ?

Rasmi Ranjan Nayak
  • 11,510
  • 29
  • 82
  • 122
  • 5
    What parameters would you expect the destructor to receive? – eerorika Feb 21 '14 at 12:44
  • 7
    `before abc a(5); gets called destructor will get called` No, it won't. – milleniumbug Feb 21 '14 at 12:50
  • 4
    I don't see how your example is relevant to your question, but there are two objects, the lifetime of both extending through the body of `main`, and at the end of `main` they are destroyed in the reverse order to that in which they were constructed. – molbdnilo Feb 21 '14 at 12:56
  • This boils down to can you overload the destructor, which the dup above answers, which is no. The [C++FAQ](http://www.parashift.com/c++-faq-lite/cant-overload-dtors.html) entry in the accepted answer is simple but says it all. – Shafik Yaghmour Feb 21 '14 at 13:14
  • Don't forget that `class != object`. There is one `class` and one or more `objects` of the same class. `ab` and `a` are `objects` of type `abc` which is a `class`. They are not the same, but they are of the same `class`. Now as described in the answers there is no need for more than one destructor, but there may be a need for more than one constructor. – rozina Feb 21 '14 at 13:37
  • There are two objects constructed differently but destroyed the same way calling destructor twice. I guess, the logic behind is - Destructor has no parameters (and only one in class), because otherwise you would have to declare corresponding destructor for every constructor. And who would clean for them if you forget to declare every needed destructor? – SChepurin Feb 21 '14 at 13:39
  • 1
    Important comment here: I see that you imply that the destructor of the first object gets called *before* the second constructor is called; This is wrong. Destructors get called when they go out of scope, in anti-chronological order. This is of critical importance, since that respects the relation your objects may have. – Joeppie Apr 03 '18 at 09:57

6 Answers6

13

A destructor doesn't have parameters, so there can be only one. However you can have more than 1 constructor since you can overload the constructor which is not possible with Destructors. Also to add that destructor is used to terminate the instance of the class and release all resources which it is using. There is nothing optional when you are destroying the object. The instance will not exist when destructor will be called.

Although a very wierd example to explain it but its like if you have 1 Apple and 1 Guava then you will use the 1 knife to cut it. ;)

Destructors are usually used to deallocate memory and do other cleanup for a class object and its class members when the object is destroyed. A destructor is called for a class object when that object passes out of scope or is explicitly deleted.

before abc a(5); gets called destructor will get called

No it will not be called as Destructors are called implicitly.

On a side note:-

However if you plan to call the Destructor explicitly(which most of the programmers will not suggest) then it would be completely your responsibility to manage the resources. The compiler will not take care of it and it may result in serious memory issues. You may check this C++ FAQ explaining it.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

The destructor cannot be overloaded because it doesn't take any parameters (nor has a return type). Its purpose is to deallocate any objects from memory that the class is using from memory. Usually, you deallocate stuff here.

The thing is that the destructor will be called via the delete or the delete[] keyword which doesn't take in parameters. Any why should they? You want to destroy the whole object, not a part of it, anyway.

Howie
  • 2,760
  • 6
  • 32
  • 60
1

The idea is that there could be different way to construct and initialize an object, therefore there can be more than just one constructor, but there is only one way to destroy objects. The language does not allow customization of the destruction of objects. All objects of a type, regardless how they are created must be destroyed in the same way.

Marius Bancila
  • 16,053
  • 9
  • 49
  • 91
0

The constructor can be overloaded using various parameter but destructor cannot be overloaded as it does not contain parameters.. This is one main reason.

And logically speaking the, you are going to delete every thing in the destructor, hence there is in fact no need of a parameter to specify anything..

51k
  • 1,381
  • 3
  • 12
  • 22
0

A destructor takes no arguments and has no return type. Its address cannot be taken. Destructors cannot be declared const, volatile, const volatile or static. So you don't have to specify arguments you passed to the constructor when the object is created.

Raging Bull
  • 18,593
  • 13
  • 50
  • 55
0

constructors can be overloaded but destructors can not be overloaded.

when an object is created, it's suitable constructor is automatically called for initializing the object. when the program is removed from memory the destructor for each object is called for removing these un referenced objects from the object pool. since destructor is the system routine for clearing the object pool.

Balayesu Chilakalapudi
  • 1,386
  • 3
  • 19
  • 43
  • Your terminology sounds a bit odd for C++. It suggests something like a memory management system like a garbage collector might be responsible for calling destructors, but there is no such thing in C++. – Hulk Feb 21 '14 at 13:03