Если вы хотите, чтобы он был универсальным, вы можете перегрузить operator <<
вместо того, чтобы фокусироваться исключительно на деструкторе и выводить биты и кусочки атрибутов вашего класса.
Вот пример:
#include <string>
#include <ostream>
#include <iostream>
class player {
std::string name;
int health;
public:
player(const std::string& n, int h) : name(n), health(h) {}
~player() { std::cout << "Destroying the following: " << *this << "\n"; }
friend std::ostream& operator << (std::ostream& os, const player& p);
};
std::ostream& operator << (std::ostream& os, const player& p)
{
os << p.name << " " << p.health;
return os;
}
int main()
{
player p1("Joe", 1);
player p2("Sam", 2);
std::cout << p1 << "\n" << p2 << "\n";
}
Вывод:
Joe 1
Sam 2
Destroying the following: Sam 2
Destroying the following: Joe 1
Вы можете следить за тем, что было сделано, посмотрев документацию здесь .