Thursday, November 15, 2007

Calling a Function Before Program's Startup

Here’s one method of calling a function even before program’s start-up. The easiest way to achieve this is by calling these functions from a constructor of a global object. Because global objects are conceptually constructed before the program's outset, these functions will run before main() starts. For example:

class Logger
{
public:
Logger()
{
StartLOgger();
}
};
Logger log; /*global instance*/

int main()
{
// Application code.
}


Here the global object log is created first of all. So the function StartLOgger() which is called in the class’s constructor is executed well before the execution of main().



Well that’s one method..There should be many others ways to achieve the same. If you are aware of other methods, please share it among us.

No comments: