• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

C++ Classes - destructors

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

Xtreme Barton

Member
Joined
Jan 17, 2004
I have an array of class objects. I setup default destructor for the class that has output inside its code block.

Problem:
When array of objects goes out of scope they obviously invoke the destructors which outputs the code written in its block. Problem is I only need one output and then the rest can be destroyed properly.

Code:
#include <string>
using namespace std;

class MyAccount
{
    public:
            void SetAccount();
            void GetAccount();
            MyAccount();
            ~MyAccount();
    private:
            string FirstName;
            string LastName;

};
Code:
#include "MyAccount.h"
#include <iostream>
using namespace std;

void MyAccount :: SetAccount(){};

void MyAccount :: GetAccount(){};

MyAccount :: MyAccount(){};

MyAccount :: ~MyAccount()
{
       cout << endl << endl << "  Programmed By - " << endl << endl;
};
Code:
#include <iostream>
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
#include "MyAccount.h"

using namespace std;

int main()
{

       MyAccount Accounts[100];

       return 0;
}
Code:
[U][B]!!!!!!!!!!!!!!!!!!      BAD OUTPUT       !!!!!!!!!!!!!!!!!!!!![/B][/U]

  Programmed By - 
  Programmed By - 
  Programmed By - 
  Programmed By - 
  Programmed By - 
  Programmed By - 
  Programmed By - 
  Programmed By - 
  Programmed By - 
  Programmed By - 
  Programmed By - 
  Programmed By - 
  Programmed By - 
  Programmed By - 

   ETC ..................................... 100 times ...................

  Press any key to continue .......
Code:
[U][B] !!!! GOOD OUTPUT !!!![/B][/U]

   Programmed By - 

   Press any key to continue .......
so you can see there is an issue about having the output in the destructor. But it is a requirement to have.

How do I avoid it ???


I was thinking right before program exit I could I could delete one object (which would only display one destructor output) then jump to a system exit (avoiding output of other destructors) ...

That doesn't sit right with me though .. Any ideas ??
 
I see that if I just add exit(0) to the destructor after output it will just exit program after one output...

technically I could probably get away with that.

But id like to know if anyone has a better idea.


EDIT:

oops said the same thing twice pretty much at the end of first post and this post. difference is i meant to delete it in main and then exit from there
rather than exiting from destructor...


I noticed that when I did try to use delete it gave an error saying it needed to be a pointer to delete an array object.


RE-CAP :
1. Output one destructor
2. Properly delete the remaining objects without output




maybe there is a way to mask the output with a no echo ..lol far reach perhaps but must explore to eliminate the idea (for now and the future) :D
 
Last edited:
Whoever told you to put output in that destructor is either a bad programmer, teacher, or both. Why is that message not just printed from main?

If you absolutely must print it from that destructor, then you might as well go with more bad practices, and set a global (or just static in that class, but since we're doing things wrong, we might as well go as wrong as possible) boolean, which is then checked in that destructor to determine whether to print the message.
 
it is from a teacher :(


and yes its mandatory or loss of points ... I also should have mentioned that I cannot add any other variables to the class



so I see what your saying about the bool and to have the option to turn it off but how would that work for multiple copies of an object ? it literally calls destructor for each copy so if you ran the first copy and turned off the ability for it to output it would only be set for that particular class .. basically running then turning off for each class after resulting in same output ..


:shrug:
 
Last edited:
so I see what your saying about the bool and to have the option to turn it off but how would that work for multiple copies of an object ? it literally calls destructor for each copy so if you ran the first copy and turned off the ability for it to output it would only be set for that particular class .. basically running then turning off for each class after resulting in same output ..


:shrug:

I said global or static.

Code:
~Destructible() {
 static bool printed = false;
 if (!printed) {
  printed = true;
  cout << "This is horrible.";
 }
}

Except I'm not sure that would work in a class that is being instantiated multiple times.

For global, you wouldn't add a variable to the class. It's not part of the class. It's global.

main.cpp
Code:
#include <blah>
bool sillyPrinted = false;
int main() {
 //blah
}
Code:
~Destructible() {
 if (!sillyPrinted) {
  sillyPrinted = true;
  cout << "This is horrible.";
 }
}
 
Ok i like what you did there !! :D

sorry I messed up trying the bool. I actually put it in except that my other cpp couldnt access it.

I added extern bool off; to the correct file and it works great !!


Thanks so much !! I feel better doing it the proper way just to go the extra steps too !!


a friggin global ...LOL !!
 
Back