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):

#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 ///:~

Thanks in advance
Ed

>> 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

GlobalMirror2.initialize(&GlobalMirror,2);

Oh! thanks very much, I should had thought about it myself! But your idea is definitely what I needed. Thx!

This article has been dead for over six months. Start a new discussion instead.