Help needed with static variabe initialization!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2006
Posts: 113
Reputation: edek is an unknown quantity at this point 
Solved Threads: 0
edek's Avatar
edek edek is offline Offline
Junior Poster

Help needed with static variabe initialization!

 
0
  #1
Oct 14th, 2006
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):

  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,502
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1479
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Help needed with static variabe initialization!

 
0
  #2
Oct 14th, 2006
>> 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
  1. GlobalMirror2.initialize(&GlobalMirror,2);
Last edited by Ancient Dragon; Oct 14th, 2006 at 8:52 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 113
Reputation: edek is an unknown quantity at this point 
Solved Threads: 0
edek's Avatar
edek edek is offline Offline
Junior Poster

Re: Help needed with static variabe initialization!

 
0
  #3
Oct 15th, 2006
Oh! thanks very much, I should had thought about it myself! But your idea is definitely what I needed. Thx!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC