How to invoke a function before main() starts

Normally main() is the first function executed in a C++ program. But there may be certain situations where we have to invoke certain functions (like hardware check) before the main() starts.

To achieve this, call the function to be invoked in the constructor of a global object.

For example, see the following code segment.

class CHardwareChecker

{

public:

CHardwareChecker()

{

CheckHardware(); // Function to be invoked before main() starts

………….

}

};

CHardwareChecker HardwareChecker; // Global object

int main()

{

……….

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: