devxlogo

Executing a Member Function After main() Terminates

You can execute any member function or any other execution after a termination of main() function by using “#pragma exit.”

Here’s the syntax:

 	#pragma exit  [priority] //withoutsemicolon

Now, here’s an example:

 #includeclass CL{public:	//A constructor	CL(){	cout << "
The object is creating"; }	void obj_func() { cout << "
This is a _function of object"; }};void func_after_main ();#pragma exit func_after_main 64   // setting a _function to execute afterthe main terminationvoid main (){  cout << "
This is main() ";}void func_after_main() {	cout << "
This is func_after_main() ";	CL obj1;	//creating an object, and _calling its constructor	obj1.obj_func();	//calling an _object's member function}

In defining such functions, the functions should not carry any attribute nor return any value. They should be defined before the #pragma statement.

Priority is optional. It is ranging from 64 to 100 (recomended).

  • 0?Highest priority used by C Libraries.
  • 63?Lowest priority used by C Libraries.
  • 64?First available Priority.
  • 100?Default priority.
  • 255?Lowest priority.

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

See also  Seven Service Boundary Mistakes That Create Technical Debt

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.