Force destructor to print only 1 statement

Hello,

I'm wondering how to force the destructor to only print 1 cout statement
mine prints all names
i made a pointer dynamic array of name and other variables and i did delete the array
but it prints all names inserted into the array
here's my code:

 
 ~Person(){cout << "Good bye " << name << endl;}
The thing about destructors is that when the object leaves scope, the destructor is called for every object. Hence why you're probably getting something like...

1
2
3
4
Good bye John
Good bye Sally
Good bye Bobby
....


If you want it to print out a specific name ONLY, you're going to need to add some logic of something along the lines of

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
~Person()
{
     if (name == "John")
         cout << "Good bye " << name << endl;

    delete [] array;
}

//Or if you want it to print out the first name in the array...

~Person()
{
   cout << "Good bye " << array[1].name << endl;

  delete[] array;
}


The gist of this: You need to indicate somehow that you want only a specific condition to print out a destructor message.

I also added in the delete[] var_name because that should be in the destructor if you're calling a destructor (only if the array is dynamically allocated, otherwise a nondynamically won't need the delete operator).
Last edited on
@Kevin2341
Thank you, i'll send you the code because i won't share it in public please read it :)

Can you please enable private messages so i can message you ?
Last edited on
The destructor gets invoked for each object that must be destroyed. If you have 5 objects in your array, then you're going to get 5 destructor calls when you delete the array. If you only want 1 print, then you'll have to move the print out of the destructor and up to where the delete is called on the array.
1
2
std::cout << "Good bye" << endl;
delete[] arrayofObjects;
Thank you @booradley60
i sent you the code so you can exactly know my problem :)
I won't be able to look over it for a couple of hours. You should post it in the thread so others may help you. Is there any reason you don't want to post the code?
I took hours writing it, and i won't my friends steel it :D
you know "google" :D
i just need to fix that destructor thing so i can run it very well
Remessage me Exculibar. I only hop on these forums every now and then, so I never had anyone PM me before. I'm only in my third semester of C++ so I can't guarantee I'll know what's going on in your code (hence a beginner being in a beginners forum, who knew?!)
Thanks anyway mate, at least u r trying to help :)
Last edited on
Will this work for you?
1
2
3
4
5
6
7
8
9
~Person()
{
    static bool firstTime = true;
    if( firstTime )
    {
         cout << "Good bye " << name << endl;
        firstTime  = false;
    }
}
@fun2code, Working like a charm!! thank you mate
Genius way :D, im sure there's another way editing my code but it require u to read the whole code and takes a lot of time but this is a great way to fix this problem :D

1 more thing, it doesnt print Good bye (name) in the last statement
it prints
statement
Good bye (name)
statement
how to make it the last one ?
Last edited on
Not sure I understand. Do you mean that "Good bye (name)" should display only when the last instance of a Person is destroyed?

If so, then one method would be to add a static int instanceCount; variable to your Person class. Initialize it to 0, increment it in every constructor, decrement it in the destructor, then print your message when instanceCount == 0. This isn't bulletproof though. You could make it fail by creating more instances of Person after destroying all current ones.

You have created a bit of a tricky problem with your requirement.
@fun2code unfortunately i can't add new members as im forced to write the code with given classes and given members by my teacher

ill pm u the code so u can take a look on it :)
thank you so much for ur great help!
Maybe the cout << "Good bye " << name << endl; code belongs in ~Student(), not ~Person(). Call getName() though since name is private in the base Person class.
I say get rid of that global variable string StuName;.
when i removed the string StuName
it prints the teacher name not the student name
and still the same problem
Student name isnt the last message

sample output of 2 courses:

You fail in Math
Good bye Exculibar
You fail in C++

what i need is

You fail in Math
You fail in C++
Good bye Exculibar

P.S Exculibar is the student name that inserted in the main
The destructor ~Person(): Print “ GoodBye “ and his name (that's what is written in the assignment provided by the teacher
Last edited on
Topic archived. No new replies allowed.