Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/articles/cpp-tutorials/)
-   -   Calling a Function Before Program's Startup (http://www.go4expert.com/articles/calling-function-programs-startup-t3492/)

pradeep 20Mar2007 17:24

Calling a Function Before Program's Startup
 
Certain applications need to invoke startup functions that run before the main program starts. For example, polling, billing, and logger functions must be invoked before the actual program begins. 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:

Code: CPP

class Logger
 {
 public:
 Logger()
 {
 activate_log();
 }
 };
 Logger log; /*global instance*/
 int main()
 {
 record * prec=read_log();
 //.. application code
 }


The global object log is constructed before main() starts. During its construction, log invokes the function activate_log(). Thus, when main() starts, it can read data from the log file.

shabbir 20Mar2007 17:53

Re: Calling a Function Before Program's Startup
 
Instead of global variables you can have some static functions as well.

pradeep 20Mar2007 17:59

Re: Calling a Function Before Program's Startup
 
Yeah! that's a good idea too!

Peter_APIIT 13Apr2007 08:22

Re: Calling a Function Before Program's Startup
 
How do implement a static function. Is it by static function name. I don't understand the concept of static.

Thanks for your help.

Your help is greatly appreciated by me and others.

shabbir 13Apr2007 11:16

Re: Calling a Function Before Program's Startup
 
Using the static keyword.

Peter_APIIT 13Apr2007 14:26

Re: Calling a Function Before Program's Startup
 
I not fully understand the concept of static. The static variable is exist even though the function has exit.

The static function pnly can call locally. I don't know this is correct concept. Please correct me if i misunderstand the concept.

Thanks ofr oyur help.


Your help is greatly appreciated by me and others.

shabbir 13Apr2007 14:40

Re: Calling a Function Before Program's Startup
 
Quote:

Originally Posted by Peter_APIIT
I not fully understand the concept of static. The static variable is exist even though the function has exit.

The concept is static variables are defined in some global space and the scope of the variable is same as the function but the existence of the variable is beyond the execution of the function.

Quote:

Originally Posted by Peter_APIIT
The static function pnly can call locally. I don't know this is correct concept. Please correct me if i misunderstand the concept.

What you mean by manually.

Peter_APIIT 14Apr2007 11:49

Re: Calling a Function Before Program's Startup
 
Quote:

What you mean by manually.
I don't get what u mean. Sorry for my stupidness.

Thanks for oyur help.

Your help is greatly appreciated by me and others.

shabbir 14Apr2007 11:53

Re: Calling a Function Before Program's Startup
 
I just wanted to know what you meant by the text in the quote?

Peter_APIIT 14Apr2007 14:01

Re: Calling a Function Before Program's Startup
 
I don't understand the text in quote.

Thanks for oyur help.


All times are GMT +5.5. The time now is 08:16.