Path: utzoo!attcan!uunet!mcsun!unido!zgdvda!sherekhan!dingelde From: dingelde@sherekhan.uucp (Dennis Dingeldein) Newsgroups: comp.lang.c++ Subject: Again: Access to argc,argv outside main() Message-ID: <3129@zgdvda.zgdvda.uucp> Date: 25 Oct 90 14:23:17 GMT Sender: news@zgdvda.zgdvda.uucp Organization: ZGDV Darmstadt, FRG Lines: 76 /* Hello again, This Code refers to my original posting. I asked there HOW DO I ACCESS ARGC AND ARGV OUTSIDE MAIN ? I've got only anwers of the type "You can't do anything before entering main(), main() is the entry point and first thing ever executed". To show that this is wrong, I send a small test. This demonstrates calling Methods/Functions before entering main(). The code shows that global objects are created before main() is entered. So there is still the problem I described originally: HOW DO I ACCESS ARGC AND ARGV OUTSIDE MAIN ? Perhaps there is no solution to this problem. Then, is it due to my compiler (g++ 1.35.1), due to me, due to C++-Design ? The following code was compiled with g++ It defines a class ("A") and create two objects of this class. The first Object ("I_will_be_called_before_main") tries to access argc, argv as mentioned by friendly people. Of course, this do not print argv[0], because this object actually is created *before* entering main. But at that time main() is not yet entered, and global_{argc,argv} are not yet defined. The second Object "I_will_be_called_after_main" is inside main(), and the lines above do initialize global_{argc,argv}. So there I get the expected result, i.e. argv[0]. */ #include int global_argc = 0; char **global_argv = NULL ; // a class class A { public: A(); }; A::A() { printf( "argc: %d ", global_argc ); if (global_argc!=0) printf( "argv[0]: %s", global_argv[0] ); printf( "\n" ); } // the test program // an object - outside main() - creates only zero, no argv A I_will_be_called_before_main ; main( int argc, char **argv ) { // at first set up argc, argv global_argc = argc ; global_argv = argv ; // an object - inside main() - creates only 1, and argv[0] A I_will_be_called_after_main ; } Dennis Dingeldein ZGDV Zentrum fuer Graphische Datenverarbeitung (dingelde@zgdvda.uucp) Wilhelminenstr. 7 wMw D-6100 Darmstadt, West Germany ~.~ Phone: +49/6151/155-101 \-/