Destructors

Hi there, could you guys teach me how to use destructors properly? can't seem to make it work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  #include <iostream>
using namespace std;
class cRectangle {
    int *a, *b;
  public:
    cRectangle(int,int);
    int area()
     {
        return *a**b;
     }
     ~cRectangle()
     {
         delete *a;
         delete *b;
     }
};

cRectangle::cRectangle (int x, int y)
{
    *a=x;
    *b=y;
}

int main () {
  cRectangle rect(3, 4);
  cRectangle rectb(6,4);
  cout << "area: " << rect.area() << endl;
  cout << "areab: " << rectb.area() << endl;
  ~cRectangle();
  return 0;
}
Last edited on
you don't need to explicitly call a destructor.

just remove line 29 :)

oh, you need to actually new up your pointers too (in your constructor perhaps?).

Or use smart pointers so you dont need to worry about deallocatin the memory.

edit: and you 'delete a', rather than 'delete *a'.
Last edited on
Hi sanasuke15,

First of all you don't call ~cRectangle(), in this case the compiler calls it when the main function terminates.
In the cRectangle constructor you pointers 'a' and 'b' are pointing to nowhere, you should allocate memory to them calling new before assigning any value:
1
2
a = new int;
b = new int;

1
2
*a = x;
*b = y;


or

1
2
a = new int(x);
b = new int(y);


Other thing is that you can't delete the content of the pointer, you have to delete the pointer:

1
2
3
4
5
~cRectangle()
     {
         delete *a;
         delete *b;
     }


write this instead:

1
2
3
4
5
~cRectangle()
     {
         delete a;
         delete b;
     }
Constructors and destructors are one of the things in C++ that make objects more complicated. You'll understand this once you take on a large assignment that requires the use a many objects.

Constructors are "automatically" called when a new instance of the object is initialized. Like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class myclass{

public:
    myclass(const int& i)
    {
         this->a_number = i; //i is not initialized, so initialize it
    }
    ~myclass()
    {
         a_number = 0; //for the sake of explanation
    }

private:
    int a_number;
};


The constructor sets the value of a_number for the first time in the instance of that object. Example:

myclass an_object(5); //an_object::a_number is now equal to 5

The constructor set the member variable, because it was executed upon initialization of that instance.

Constructors and destructors are basically functions that are executed when an instance of the class is created and destroyed. You can not call them.

when the scope of the class ends, or it is destructed (using delete on a pointer to an instance of the class will call the destructor before deallocating memory), the destructor is called. In this case, the destructor will set the integer member in myclass to 0.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void demonstration()
{
    myclass an_instance(4);
    
    //whatever...

    //before the function ends, the destructor is called.  You don't need to do anything.
}


int main()
{
    void demonstration();
    /* myclass was initialized and destructed!  :) */
    return 0;
}


If you have any questions, feel free to ask.

Also, one more thing: When handling pointers, remember that when you call delete, you are deleting what that pointer points to, NOT the pointer itself. The pointer is still valid, but it will point to empty/unallocated space. Set them to NULL after deleting if you plan on referencing them afterwards (so you know whether they are allocated/deallocated).

Examples:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int *myint = NULL; //nothing in there...
int a_number(5); //a_number.

myint = &a_number; //myint points to a_number.  You don't want to delete this!!  The destructor of myint will do it for you

(*myint)++; //parentheses for emphasis on operator precedence
//a_number is incremented

myint = new int(10); //allocated space for a new integer.  not you WILL want to delete it.

*myint = a_number;  //this new number is set equal to the value of a_number.

delete myint; //delete the allocated space.
myint = NULL;  //make sure we know it was deleted if we want to use the pointer again.

myint = &a_number.  //doesn't matter in the slightest.  Memory is stored at a_number and will be deleted upon it's destruction. 


I hope you understand pointers a bit better.
Last edited on
Constructors and destructors are one of the things in C++ that make objects more complicated. You'll understand this once you take on a large assignment that requires the use a many objects.

Um... what? Constructors and destructors are things that make it much, much more straightforward to use large numbers of objects in large projects. Being able to ensure objects are created in a known state, and being able to ensure that they can release resources and do any other tidyup tasks automatically when they're deleted, makes things much simpler!

Iwishiknew. Lol man, you say some crazy stuff :)
While Destructors are pretty straight forward, Constructors can get a little fuzzy at times: http://www.cplusplus.com/forum/lounge/116427/

Last edited on
I know that constructors/destructors make life so much easier.

BUT there are some things that make them a pain in the ass. If you have a huge project, and all these objects, constructors and destructors create different kinds of errors than if everything was written out as a function. You would literally find yourself checking all the objects where the error occures it it segfaults.

I have always been able to wrat out bugs from my program until I started working with classes. Classes are much MUCH different than working with functions. You can write everything in functions, it just tends to be a bit more messy, but you know exactly where a bug is if one occures.

If you have 10 different objects "interacting" with eachother, then a segfault would force you to trace through a lot of code.
Last edited on
IWishIKnew wrote:
Constructors and destructors are basically functions that are executed when an instance of the class is created and destroyed. You can not call them.
You can call constructors like
1
2
3
void f(std::string s);
...
f(std::string());
but you cannot call a constructor as a member function, although you can with destructors.
1
2
3
std::string s;
...
s.~string();
Normally you shouldn't call a dtor explicitly like that, but it is necessary in some occasions such as destroying a single object in a memory block.

If you have 10 different objects "interacting" with eachother, then a segfault would force you to trace through a lot of code.
Or you can use a debugger and find out exactly where the problem is.
Last edited on
Constructors and destructors are one of the things in C++ that make objects more complicated.
I know that constructors/destructors make life so much easier.


whoa...
Complicated != easier. That said, I'm not contradicting myself. It's not like I said "harder" or "mor difficult".

I am right though: they introduce a new train of thought. I programmed in nothing but functions until I learned how to write classes, and boy was it easier. Unfortunately, if I encountered a nasty bug, it would be more difficult to trace if I was using a lot of objects.
Topic archived. No new replies allowed.