c++ - Calling func before main()
- Karol Gottan (8/8) Feb 01 2004 Hi,
- Jan Knepper (17/31) Feb 01 2004 Very simple... ;-)
- Karol Gottan (10/11) Feb 01 2004 Unfortunately I am getting this :
- Scott Michel (3/15) Feb 02 2004 You're not compiling C++ code, are you?
- Karol Gottan (6/7) Feb 02 2004 In my first attempt yes - I did not compile in C++ mode.
- Jan Knepper (6/49) Feb 01 2004 --
- Karol Gottan (4/7) Feb 01 2004 Thanks ! Works perfectly !
- Steve Strand (11/11) Feb 01 2004 Another way is to define a class at global level that has a constructor.
- Scott Michel (4/17) Feb 01 2004 Before someone takes the initiative and starts elaborating on this examp...
-
Jan Knepper
(9/29)
Feb 01 2004
I was thinking that the question would come from a C++ classes book
Hi, I am looking for a way how to call a desired function before main() is called in C mode ? Something like #pragma startup foo in other compilers. -- Karol Gottan
Feb 01 2004
Very simple... ;-) static int result_of_foo = foo (); int main ( int, char **, char ** ) { return ( 0 ); } static int foo () { // Do something before 'main' is being invoked. return ( 0 ); } HTH Karol Gottan wrote:Hi, I am looking for a way how to call a desired function before main() is called in C mode ? Something like #pragma startup foo in other compilers. -- Karol Gottan-- ManiaC++ Jan Knepper But as for me and my household, we shall use Mozilla... www.mozilla.org
Feb 01 2004
<jan smartsoft.us> wrote :Very simple... ;-)Unfortunately I am getting this : ------------ static int result_of_foo = foo (); ^ test.c(3) : Error: constant initializer expected ------------ Thanks for jumping in. -- Karol
Feb 01 2004
Karol Gottan wrote:<jan smartsoft.us> wrote :You're not compiling C++ code, are you? -scooterVery simple... ;-)Unfortunately I am getting this : ------------ static int result_of_foo = foo (); ^ test.c(3) : Error: constant initializer expected ------------
Feb 02 2004
<scottm cs.ucla.edu> wrote : [...]You're not compiling C++ code, are you?In my first attempt yes - I did not compile in C++ mode. But then I realised I did it wrong and added -cpp. -- Karol
Feb 02 2004
Jan Knepper wrote:Very simple... ;-)static int foo ();static int result_of_foo = foo (); int main ( int, char **, char ** ) { return ( 0 ); } static int foo () { // Do something before 'main' is being invoked. return ( 0 ); } HTH Karol Gottan wrote:-- ManiaC++ Jan Knepper But as for me and my household, we shall use Mozilla... www.mozilla.orgHi, I am looking for a way how to call a desired function before main() is called in C mode ? Something like #pragma startup foo in other compilers. -- Karol Gottan
Feb 01 2004
<jan smartsoft.us> wrote :Jan Knepper wrote:Thanks ! Works perfectly ! -- KarolVery simple... ;-)static int foo ();
Feb 01 2004
Another way is to define a class at global level that has a constructor. The constructor will be called before main(). Example: struct foo { foo() {cout << "do stuff before main\n";} ~foo() {cout << "do stuff after main\n";} } myfoo; int main() { cout << "here we are in main\n"; }
Feb 01 2004
Before someone takes the initiative and starts elaborating on this example, this question is a comp.lang.c++ FAQ item, whihc also covers how to sequence object allocation and construction before main() is called. Steve Strand <snstrand comcast.net> wrote:Another way is to define a class at global level that has a constructor. The constructor will be called before main(). Example: struct foo { foo() {cout << "do stuff before main\n";} ~foo() {cout << "do stuff after main\n";} } myfoo; int main() { cout << "here we are in main\n"; }
Feb 01 2004
<g> I was thinking that the question would come from a C++ classes book or something like that. It is one of the standard questions employers will ask you during a technical job interview for a C/C++ coding job... Scott Michel wrote:Before someone takes the initiative and starts elaborating on this example, this question is a comp.lang.c++ FAQ item, whihc also covers how to sequence object allocation and construction before main() is called. Steve Strand <snstrand comcast.net> wrote:-- ManiaC++ Jan Knepper But as for me and my household, we shall use Mozilla... www.mozilla.orgAnother way is to define a class at global level that has a constructor. The constructor will be called before main(). Example: struct foo { foo() {cout << "do stuff before main\n";} ~foo() {cout << "do stuff after main\n";} } myfoo; int main() { cout << "here we are in main\n"; }
Feb 01 2004