There is a function that prints something and I need to call this function both explicitly in the main as well as in the destructor when the object (has to be a global one) is destroyed. However the destructor's call doesn't display in the file.
#include <iostream>
#include <fstream>
using namespace std;
class A {
public:
friend void func(A&);
~A() {
func(*this);
}
};
A a;
ofstream out;
int main() {
out.open("file.txt");
func(a);
cin.get();
return 0;
}
void func(A& a) {
out << "\nHello!!!\n";
}
I only see one "Hello!!!" in the file when I want two. I also haven't closed the ostream object either. Ideally, I'd want to close it after the function call in the destructor. How do I do it?