G'day,
Using C++, I would like to have a global object which contains several public member objects which can be accessed “through” the global object, while at the same time the code in the member objects should be able to call methods in the global object by accessing it as a global object (i.e. without passing a specific reference to the global object into the member objects).
I thought this would be quite straight forward, along the lines of my simplified example code below, but it won’t compile. Within the IDE, while viewing the header files I get “redefinition of GlobalObject / MemberObject” and "unknown type name" warnings (despite the “pragma once” in each header file ?). And when I compile the code the compiler reports “GlobalObject / MemberObject is undefined” errors.
I guess I have some sort of circular reference problem, though to be honest I don’t really understand why, as while the global object contains a reference to the member object(s), the member object(s) don’t contain a reference to the global object because I intend them to access the global object in the same way any other code would access it directly from the main function…? Doesn’t making GlobalObject global by declaring it outside of the main function then referencing it as external in the MemberObject header file do this, or am I mistaken here ?
I know people have strong opinions about the evil of global objects, but I believe there are circumstances where they are appropriate, especially in embedded systems with limited resources and which essentially “start-up” and never “shut-down” (or at least don’t shut-down in a controlled manner - the power just goes off). So I would like to know how what I am trying to do can be made to work, or if it can’t, I would like to understand why not.
I would appreciate any guidance anyone might like to offer, thank you.
GlobalObject.h
#pragma once
#include "MemberObject.h"
class GlobalObject
{
  private:
    int               Data;
    int               Total;
  public:
    GlobalObject();
    MemberObject      MO;
    int               GetData(void);
    void              SetData(int NewData);
    int               GetTotal(void);
    void              SetTotal(int NewTotal);
};
GlobalObject.cpp
#include "GlobalObject.h"
GlobalObject::GlobalObject() : MO()       { Data = 0; Total = 0; }
int  GlobalObject::GetData(void)          { return Data; }
void GlobalObject::SetData(int NewData)   { Data = NewData; }
int  GlobalObject::GetTotal(void)         { return Total; }
void GlobalObject::SetTotal(int NewTotal) { Total = NewTotal; }
MemberObject.h
#pragma once
#include "GlobalObject.h"
extern GlobalObject GO;
class MemberObject
{
  private:
    int               Data;
  public:
    MemberObject();
    int               GetData(void);
    void              SetData(int NewData);
    void              SetGlobalTotal(void);
};
MemberObject.cpp
#include "MemberObject.h"
MemberObject::MemberObject()              { Data = 0; }
int  MemberObject::GetData(void)          { return Data; }
void MemberObject::SetData(int NewData)   { Data = NewData; }
void MemberObject::SetGlobalTotal(void)   { GO.SetTotal(GO.GetData() + Data); }
main.cpp
#include "GlobalObject.h"
GlobalObject GO;
int main(void)
{
  GO.SetData(1000);
  GO.MO.SetData(2000);
  GO.MO.SetGlobalTotal();
}
 
     
     
     
    