1

I'm pretty new to C++ language. I'm using it with Qt to create a cross-platform user interface. The user interface will show different graphs and plots by using the same data. So I would like to have my data stored in a class (Data_Class) accessible from all the other classes. In the data class, I am using private static variables and public static methods (get/set).

What is not clear to me is how to access the data class from the other classes. In other words, how to get data values in Class_B (from Data_Class) after I write them in Class_A (to Data_Class).

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Mikael
  • 183
  • 3
  • 4
  • 11

2 Answers2

3

Assuming that this is syntax an oriented question:

class Data_Class {
public:
    static int a;
    static int GetA() const { return a; }
}

In ClassA:

Data_Class::a;
Data_Class::GetA();

Don't forget to add Data_Class static members definitions, for example into dataclass.cpp:

static int Data_Class::a = 0;

Otherwise you'll get an undefined reference linker error.


You also may consider using singleton pattern (according to comments, singleton is bad idea and you should avoid it if possible) as Aubin suggested which will take care of adding new static member definitions:

class Data_Class {
private:
    Data_Class();  // Prevents multiple instances

    int a;
    int b;

public:
    // Will initialize instance if needed
    static Data_Class &GetInstance(){
       static Data_Class instance;
       return instance;
    }


    int GetA() const {return a;}
};

Usage:

Data_Class::GetInstance().GetA()

Or go with little more complex but more flexible Dependency injection which is according to wiki:

Dependency injection is a software design pattern that allows a choice of component to be made at run-time rather than compile time. This can be used, for example, as a simple way to load plugins dynamically or to choose mock objects in test environments vs. real objects in production environments. This software design pattern injects the dependent element (object or value etc) to the destination automatically by knowing the requirement of the destination.

Community
  • 1
  • 1
Vyktor
  • 20,559
  • 6
  • 64
  • 96
  • 1
    @DeadMG if you have better idea for such a simple task I'd glad to add it to my answer (I've provided more options... it's his job to study each of them and decide which one (not) use)... – Vyktor Oct 29 '12 at 07:04
  • @DeadMG and could you please provide source that explains why singleton is such a bad idea for small projects? – Vyktor Oct 29 '12 at 07:42
  • -2 - If you really need to use the singleton pattern, then at least don't do this `new/delete` rubbish. Hint: how do you know your application is closing and needs to call `DestroyInstance`? Hint: When is a static function-local variable initialized? Hint: Does C++ have a construct far superior to pointers if the pointer in question cannot (or should not) **ever** be `NULL`? Hint: Does C++ have means for memory allocation far superior to dynamic allocation if appropriate? – Christian Rau Oct 29 '12 at 08:07
  • @Vyktor Yes, now you got static initialization order fiasco. Don't make the instance a member of the class, but a function-local variable. – Christian Rau Oct 29 '12 at 08:18
  • Thank you. My problem was that I didn't do any initialization in the dataclass.cpp file. This fixed my issue: static int Data_Class::a = 0; – Mikael Oct 29 '12 at 08:43
  • @Mikael I though so... But still I would consider using *Dependency injection* if I were you – Vyktor Oct 29 '12 at 08:44
1

Given these two class declarations:

class A {
   public:
      static int get_data();
};

class B {
   public:
      static void do_something();
};

You can access A's methods from B like this:

void B::do_something()
{
     int the_data = A::get_data();
     // now do whatever you need to do with that data
};
Tom W
  • 1,304
  • 8
  • 9
  • 1
    You shouldn't have the `static` on the definition, just the declaration. IIRC, the definition outside of the class won't compile with it. – chris Oct 28 '12 at 19:08