I have C++ that follows this pattern in an include file:
#ifdef MYHEADER
extern ClassA globalA;
#endif
#ifndef MYHEADER
#define MYHEADER
class ClassA {
// code for ClassA
};
static ClassA globalA;
#endif
The desire is to have only one instance of ClassA (globalA) where it is defined only in the header file. This is old code that I'm trying to clean up without making mass changes.
The problem I'm seeing is that in the debugger there are (at least) two different instances of globalA (two different addresses). I searched for other declarations and even commented out the static declaration to make sure I get a link error (I did). This code is threaded.
Is this a valid pattern? What might I misunderstood? Is there a better way to do this (without requiring changes to all references of globalA) ?