| | |
Help needed with static variabe initialization!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Basicly I need to know how to refer to declared external static variables (GlobalMirror, GlobalMirror2 etc.) inside the class 'Initializer' and initialize them. The following code doesnt work (compiler doesnt see my 'extern' declarations inside the class Initializer):
Thanks in advance
Ed
C++ Syntax (Toggle Plain Text)
#ifndef INITIALIZER_H #define INITIALIZER_H #include "Mirror.h" #include <iostream> extern Mirror GlobalMirror; extern Mirror GlobalMirror2; extern Mirror GlobalMirror3; extern Mirror GlobalMirror4; extern Mirror GlobalMirror5; class Initializer { static int initCount; public: Initializer() { std::cout << "Initializer()" << std::endl; // Initialize first time only if(initCount++ == 0) { std::cout << "performing initialization" << std::endl; //Initialization in expected and PREDICTABLE order: //ALL 5 FOLLOWING LINES ARE ERRORS: (compiler doesnt 'see' my 'extern' declarations which //are above) GlobalMirror(1); GlobalMirror2(&GlobalMirror,2); //each object is initialized with GlobalMirror3(&GlobalMirror2,3); //the previous one GlobalMirror4(&GlobalMirror3,4); GlobalMirror5(&GlobalMirror4,5); } } ~Initializer() { std::cout << "~Initializer()" << std::endl; // Clean up last time only if(--initCount == 0) { std::cout << "performing cleanup" << std::endl; // Any necessary cleanup here } } }; static Initializer init; #endif // INITIALIZER_H ///:~
Ed
Last edited by edek; Oct 14th, 2006 at 7:19 pm.
>> GlobalMirror2(&GlobalMirror,2);
You can't do that after the object has been constructed. You can only do it when actually instantiating the object. You need to write an initializer method or other method that can be called to initialize those classes, something like this
You can't do that after the object has been constructed. You can only do it when actually instantiating the object. You need to write an initializer method or other method that can be called to initialize those classes, something like this
C++ Syntax (Toggle Plain Text)
GlobalMirror2.initialize(&GlobalMirror,2);
Last edited by Ancient Dragon; Oct 14th, 2006 at 8:52 pm.
![]() |
Similar Threads
- bankaccount program (Java)
- Helping with calling a function (C)
- Last time I ask for help, Help me please (C++)
- how VC++ copes with 'static initialization dependency'? (C++)
- Help figuring out (C++)
- Error Linking KeyLogger.exe (C++)
Other Threads in the C++ Forum
- Previous Thread: pyramid of numbers
- Next Thread: Bad ol' ship
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline graph homeworkhelper iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg simple sorting string strings template text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






