| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
I'm not very experienced with C++, but I am writing a practical application for my internship job. So you can imagine how nervous I am now since it crashes everytime I run it =)
This is a very simple class for coordinate objects, consisting of an interger x and y value, as well as some basic geometry functions. My program works when I'm not initializing a lot of these objects, but when I do it crashes and Windows NT gives me a program error warning. I've traced my program and it seems to be a problem with the destructor. The program always freezes when it tries to exit the destructor for a certain object after a while. Hence I think that I might be using too much memory from constructing all these objects. Right now my destructor is empty, since all the C++ tutorials I've found online only specify how to free memory for pointers and arrays. I'd be very glad if someone could tell me what is wrong or how to solve this problem ^_^ Here are my constructor and destructor methods for reference: coor::coor() //default paramenters set to (0,0) { x= 0; y= 0; } coor::coor(int new_x, int new_y) { x = new_x; y = new_y; } coor::~coor() //destructor is empty { } Thanks! |
|
#2
|
|||
|
|||
|
Solution
Though no one replied, I solved my problem a while later, and I'll post the solution here just in case someone had a similar question.
I asked this same question on another forum and got some useful answers from there too. The main problem with the crashing was not in the destructor, but in the assignment operator overloading. My original assignment overloading code included a delete statement, which was there in the first place since I got that code from a website. My program worked fine when I took out those delete statements. What people told me was that since I didn't have any arrays or pointers in my class, I didn't have to specify a destructor for them or delete them in my assignment overloading. The compiler would take care of these built in types such as int, char etc. The delete command is only used for pointers and arrays. |
|
#3
|
|||
|
|||
|
When you are using scalar data types, the default destructor will clean house for you.
Maybe a matter of preference, but why don't you place your default values in the parameter listing within your specification file for your class. When the object is called with no arguments, these values are automatically assigned. coor (int = 0, int = 0); |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Question regarding destructors |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|