7

I'm learning C++ and moving my project from C to C++. In the process, I stumbled on this problem: how to save/update variables that are in use in several classes? In C I used global variables, but it is not good for C++.

So, let's assume we have 4 classes:

class Main_Window
{
    //...
    void load_data_menu_selected();
}

class Data
{
    //...
    double *data;
}

class Load_Data
{
    //...
    double *get_filename_and_load();
}

class Calculate
{
    //...
    int do_calculation()
}

So, Main_Window is class for application's main window where it interacts with user input etc.
I want to do:

  • create an instance of class Data in the Main_Window
  • use Load_Data for loading data from file and store it in the Data
  • use Calculation class for doing something with read data in Data class
  • The question is: where I should create classes, to make Data class members available from other classes. Should I use Inheritance?

    Daniel Uspensky
    • 168
    • 1
    • 1
    • 5
    • Here some info about classes in general: http://cplusplus.com/doc/tutorial/classes/ – RvdK Dec 20 '10 at 10:35
    • get and set methods, but I'd suggest one may need to rethink their code design a bit here. – ewanm89 Dec 20 '10 at 10:37
    • 2
      Class names like `Load_Data` and `Calculate` are very suspicious. Classes are nouns, not verbs. You might have a `DataLoader`, but chances are you don't need anything that complicated - the `Data` constructor is all you need most of the time, and a free function `load_from_filename` at file scope is all you need almost all of the rest of the time. – Karl Knechtel Dec 20 '10 at 11:33

    5 Answers5

    2

    Start from observing what are possible relations between instances of two classes. Let us say a is an instance of class A and b is an instance of class B. If a uses b, class A can have as its member instance of class B (b), pointer to b (which is of type B*), or reference of b (which is of type B&). If only one method of class A uses b, you have again same three options: B, B* or B& can be method's arguments. Having B* and B& as class members suggests that a does not control b's lifetime so class A must have a method that sets these members through its parameters. The question of ownership (objects' lifetimes) has a big role in design of relationship between classes. Main relationships are briefly described in this article.

    Bojan Komazec
    • 9,216
    • 2
    • 41
    • 51
    1

    I think you only want to have a Main_Window class, and the rest should be members of that class.

    class Main_Window
    {
     private:
     DataObject windowData;
    
     public:
     void loadData(string fileName);
     void calculate();
    }
    

    Inside the loadData and calculate methods, you will be able to access the same data with this->windowData . Sorry if my syntax is bad, my c++ is rusty

    Matt
    • 3,778
    • 2
    • 28
    • 32
    • I thought about it, but all these classes are large and have many methods. It is not very convenient to merge them... – Daniel Uspensky Dec 20 '10 at 10:45
    • I don't think merging ever is. However, i'm pretty certain that "Load_Data" and "Calculate" should not be classes. Sure you can do it, but you can also store all your ints and floats as strings. Probably not the best idea. – Matt Dec 20 '10 at 12:57
    1

    Typically, you would pass (const) Data& around as an argument. If do_calculation() needs a Data to work with, then it takes Data&. But I can't really be more specific or useful unless you post more of your design.

    Puppy
    • 144,682
    • 38
    • 256
    • 465
    0

    You need to know how to design in OO. Thinking in C is different from thinking in c++. You can that your classes have many methods. Well, that sound like a bad design.

    I can recommend you to start with the SOLID principle. Then start writing unit tests for your classes. TDD could help you improve your design even further.

    BЈовић
    • 62,405
    • 41
    • 173
    • 273
    0

    It sounds like you should not use inheritance here. The main reason for saying so is that you have a number of classes (Window, Calculator, etc.) using or doing something to an entity (i.e. Data). Inheritance is used to denote an "is a" relationship (i.e. if A inherits from B, A "is a" B).

    In this case, you use composition, which denotes a "has a" relationship. So each class takes a reference to an instance of Data, and acts upon that object.

    Who owns the Data object? To share a single Data object, you might want to look into Boost shared_ptr, which allows multiple reference-counting pointers to share an object allocated with "new".

    axw
    • 6,908
    • 1
    • 24
    • 14