In the C language, if you use GCC, you can declare constructor and destructor with the attribute keyword (how to execute a function before the main function starts in the C language)
#include <stdio.h>__attribute((Constructor))voidBefore_main(){Printf("%s/n",__function__);}__attribute ((destructor) void after_main () {printf ( "%s/n" ,__function__); } int main (int Argcchar ** argv ) {printf ( "%s/n" __function__); return 0 /span>
In C + +, using the attributes of global variables and constructors, the constructor of a global variable executes (how C + + language executes a piece of code before the main function executes)
#include <Iostream>UsingNamespaceStd;ClassTestClass{publictestclass (); }; testclass::testclass () { cout<< "TestClass" << endl;} testclass ts; Define a global variable so that the code inside the class executes before main int main () { cout<< "main" <<endl return 0;}
How do I execute a statement before the main () function in C + +?