-1

I hope that I am providing enough information and that I've titled this correctly.

In general, I want to have a class that stores data in my application, and I need several other classes to access the same data. essentially sharing the data between multiple classes.

Short/Concise Code follows:

example.cpp (main application)

// example.cpp : Defines the entry point for the console application.
//
#include "AnotherClass.h"
#include "ObjectClass.h"
#include "stdafx.h"
#include <iostream>  
#include <iomanip>

using namespace std;  
//Prototype
static void do_example ( );

int main()  

{  
    do_example ( );
}

static void do_example ( ) {


    MyObject.a = 5;
    cout <<"MyObject.a value set in main application is "<<MyObject.a<<"\n";

    AnotherClass    m_AnotherClass;

    m_AnotherClass.PrintValue();

}       

ObjectClass.h

class ObjectClass {
public:
    ObjectClass(); // Constructor
    int a; // Public variable
} static MyObject;

ObjecClass.cpp

#include "ObjectClass.h"

ObjectClass::ObjectClass() {

    a = 0;

}

AnotherClass.h

class AnotherClass {
public:
    AnotherClass(); // Constructor

    void PrintValue(); // Public function
    int value; // Public variable

};

AnotherClass.cpp

#include "AnotherClass.h"
#include "ObjectClass.h"
#include "stdafx.h"
#include <iostream>  
#include <iomanip>

using namespace std; 

AnotherClass::AnotherClass() {

    value = MyObject.a;

}

void AnotherClass::PrintValue() {

    cout <<"value in AnotherClass is "<<value<<"\n";
    cout <<"it should be the same."<<"\n";
}

But the value is the default value of 0, as if it is a new instance of MyObject. But it should be pulling the value of 5 from the static MyObject.

What am I missing?

JeffS
  • 327
  • 7
  • 17
  • 2
    Could you post some real, compilable code that reproduces your problem please? – Mat Apr 17 '12 at 18:42
  • You are looking for the [Singleton design pattern](http://stackoverflow.com/questions/1008019/c-singleton-design-pattern) – Silas Parker Apr 17 '12 at 18:59
  • Short / Concise Compilable code added. It's really pretty simple code. It should work. – JeffS Apr 17 '12 at 20:07

1 Answers1

3

A static class instance is a static variable itself. What you expect to happen also makes sense, however, your code does not show how the static instance is handled. In fact, if both MyClassInstances refer to the same object, then you don't even need static declaration.

Also, static variables are defined in cpp files. If you define it in a header, the cpp file (compilation unit) that includes it will define a separate static variable. So, define the static object in the main cpp file and use extern MyStaticClass in the header file. The linker will then link the uses to the same variable.

perreal
  • 94,503
  • 21
  • 155
  • 181
  • SCC has been added above. It's pretty simple, but doesn't appear to treat the static object as containing the same data. – JeffS Apr 17 '12 at 20:06
  • You need to define the static variable in exactly one cpp file and use `extern` in the header. The way you have it creates a separate object for each compilation unit. – perreal Apr 17 '12 at 20:25
  • You are correct, extern defined variables are static and do not need to be declared as such. I added extern Objectclass MyObject; to my AnotherClass.cpp and removed the static instance in the ObjectClass.h. Then I added a non-static ObjectClass MyObject declaration in the example.cpp (main). it all is working as intended. thanks :) – JeffS Apr 18 '12 at 14:28