944,147 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2088
  • C++ RSS
Oct 14th, 2006
0

Help needed with static variabe initialization!

Expand Post »
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):

C++ Syntax (Toggle Plain Text)
  1.  
  2. #ifndef INITIALIZER_H
  3. #define INITIALIZER_H
  4. #include "Mirror.h"
  5. #include <iostream>
  6.  
  7. extern Mirror GlobalMirror;
  8. extern Mirror GlobalMirror2;
  9. extern Mirror GlobalMirror3;
  10. extern Mirror GlobalMirror4;
  11. extern Mirror GlobalMirror5;
  12.  
  13. class Initializer {
  14. static int initCount;
  15. public:
  16. Initializer() {
  17. std::cout << "Initializer()" << std::endl;
  18. // Initialize first time only
  19. if(initCount++ == 0) {
  20. std::cout << "performing initialization"
  21. << std::endl;
  22. //Initialization in expected and PREDICTABLE order:
  23. //ALL 5 FOLLOWING LINES ARE ERRORS: (compiler doesnt 'see' my 'extern' declarations which
  24. //are above)
  25. GlobalMirror(1);
  26. GlobalMirror2(&GlobalMirror,2); //each object is initialized with
  27. GlobalMirror3(&GlobalMirror2,3); //the previous one
  28. GlobalMirror4(&GlobalMirror3,4);
  29. GlobalMirror5(&GlobalMirror4,5);
  30. }
  31. }
  32. ~Initializer() {
  33. std::cout << "~Initializer()" << std::endl;
  34. // Clean up last time only
  35. if(--initCount == 0) {
  36. std::cout << "performing cleanup"
  37. << std::endl;
  38. // Any necessary cleanup here
  39. }
  40. }
  41. };
  42.  
  43. static Initializer init;
  44. #endif // INITIALIZER_H ///:~
Thanks in advance
Ed
Last edited by edek; Oct 14th, 2006 at 7:19 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
edek is offline Offline
113 posts
since Oct 2006
Oct 14th, 2006
0

Re: Help needed with static variabe initialization!

>> 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
C++ Syntax (Toggle Plain Text)
  1. GlobalMirror2.initialize(&GlobalMirror,2);
Last edited by Ancient Dragon; Oct 14th, 2006 at 8:52 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Oct 15th, 2006
0

Re: Help needed with static variabe initialization!

Oh! thanks very much, I should had thought about it myself! But your idea is definitely what I needed. Thx!
Reputation Points: 10
Solved Threads: 0
Junior Poster
edek is offline Offline
113 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: pyramid of numbers
Next Thread in C++ Forum Timeline: Bad ol' ship





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC