I want to call destructor of an instance (proc) always before my program ends, especially after return 1 or exit() in main.
I found C++ function atexit(), but it requires pointer to void function with no argument, so the code below cannot be compiled. How I can solve it, please?
Destructor of my instance requires MySQL connection.
#include <WinSock.h>
#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <mysql.h>
#include <string>
// Declarations for Mysql DB
using namespace std;
class Process {
public:
~Process();
};
Process::~Process ()
{
// Interaction with DB
}
int main(void)
{
// Join to DB
atexit(proc.~Process); // Call desctructor of instance proc before program ends
Process proc;
// App code
return 0;
}
prochas automatic duration, i.e. when exitingmain, it will be destroyed automatically (and the destructor invoked) – you don’t need theatexitbusiness..Unless as @Rob mentions below, you call
exit()in your code somewhere… if that’s the case, then you’ll have to allocateProcesson the heap, provide a function thatatexitcan call which is aware of this instance, and delete it there…