From: Oliver Regenfelder on 5 Jun 2010 06:49 Hello, RB wrote: > // Global variables > int Global_Int ; > > //--------Begin Class --------// > class Class1 > { > // Static Member Variables > static int Class1_Static_Int; > static StaticStruct classClass1; > Class1( ) {} > ~Class1( ) {} > > Class1(int IntArg) { > Class1_Int = IntArg; > Global_Int = IntArg; // appears I can initiaze a global var inside constructor > Class1_Static_Int = IntArg; // but static vars appears must first be initialized > // outside the class (and after class) at global location > } You should get used to setup class members in an initialising list like in: Class1(int IntArg) : Class1_Int(IntArg) This doesn't do much for PODs but in if your class variables are themselfs classes this becomes important (exception safety). Best regards, Oliver
From: RB on 5 Jun 2010 08:59 > You should get used to setup class members in an initialising list > like in: > > Class1(int IntArg) > : Class1_Int(IntArg) > > This doesn't do much for PODs but in if your class variables are > themselfs classes this becomes important (exception safety). > Best regards, Oliver Thanks Oliver, I have read about initializing with the colon: and do plan to implement that. On this particular code I was just experimenting which I've been doing a lot of lately just to learn the ins and outs. Thanks again for replying. And to be complete in my former comments, a static var could be initialized in the constructor "if it was not a class member" which would make it a just another global var with the exception of having only file scope as you pointed out to me.
From: Doug Harrison [MVP] on 5 Jun 2010 15:56 On Sat, 5 Jun 2010 08:59:51 -0400, "RB" <NoMail(a)NoSpam> wrote: >And to be complete in my former >comments, a static var could be initialized in the constructor >"if it was not a class member" which would make it a just >another global var with the exception of having only file >scope as you pointed out to me. A couple of things. The use of "static" to confer "file scope" is deprecated. Its replacement is the anonymous namespace, e.g. namespace { int x; class Y { ... }; } Note that you can put classes inside the anonymous namespace, which is very important when defining classes for use inside just one file. It avoids very weird problems when there are multiple such classes with the same name. The "static" keyword never could deal with these violations of the One Definition Rule (ODR). Second, it usually wouldn't make sense to initialize a static object inside a constructor for a class X. The reason of course is that the static object would be initialized every time an X was created. However, I guess it could potentially make sense to do something like: namespace { int x; int y; int z; class Initializer { public: Initializer() { // Do some complicated initialization of x, y, and z // that is inconvenient to do with the normal syntax // int x = whatever1; // int y = whatever2; // int z = whatever3; } }; Initializer init; } Note that only one Initializer is ever created. Note also that within a single translation unit, initialization of namespace scope objects proceeds from top to bottom. While x-z are ints, notionally they are initialized, and if they had class type, and the class had a ctor, the ctor would be run before the one for "init". Therefore, it's important that "init" be declared last, after all the variables it's going to "initialize" (really it's assignment, not initialization). -- Doug Harrison Visual C++ MVP
From: RB on 6 Jun 2010 08:09 Thanks Doug for further explanation: 4 quick questions: If I use namespace nSpace1{ int nInt; } cout << nSpace::nInt ; Is the scope of this namespace is file, correct ? And to use it in another file I would have to put using namespace nSpace; // correct ? And then if I did have the using namespace nSpace; (in another file or the declared file) I could just write cout << nInt ; // correct ? "if " I am correct so far, I would at this point surmise that one could further put this in included header files as, #ifndef <nSpace> #define using namespace nSpace; #endif Correct ? And finally I would surmise that namespace is to define a type of scope ( Label reference? for lack of a better description ) but not lifetime or duration which is still decided by other factors. Correct ?
From: Doug Harrison [MVP] on 6 Jun 2010 14:50 On Sun, 6 Jun 2010 08:09:15 -0400, "RB" <NoMail(a)NoSpam> wrote: >Thanks Doug for further explanation: >4 quick questions: >If I use > >namespace nSpace1{ int nInt; } >cout << nSpace::nInt ; > >Is the scope of this namespace is file, correct ? No, the namespace has global scope, the global scope itself being an unnamed namespace. >And to use it in another file I would have to put >using namespace nSpace; // correct ? > >And then if I did have the using namespace nSpace; >(in another file or the declared file) >I could just write > >cout << nInt ; // correct ? No. The compiler has to see the declaration before you can use it. To use nInt in multiple translation units without creating multiple objects and causing link-time multiple definition errors, it would have to be declared extern and defined in exactly one place, just like a global variable. >"if " I am correct so far, I would at this point surmise that >one could further put this in included header files as, > >#ifndef <nSpace> >#define using namespace nSpace; >#endif > >Correct ? No. Namespace names aren't macros and thus can't be used with #if, and I have no idea what you're trying to do with "#define using". You shouldn't redefine keywords. >And finally I would surmise that namespace is to define a type >of scope ( Label reference? for lack of a better description ) >but not lifetime or duration which is still decided by other factors. >Correct ? Right, but it's simpler perhaps than you imply. Namespaces exist only in the global scope or inside other namespaces, so all objects defined in a namespace have static storage duration. -- Doug Harrison Visual C++ MVP
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 5 Prev: Server Busy message during printing from Excel after Off. 2003 SP2 Next: VS2010 and HINSTANCES |