简体   繁体   中英

How to exchange data between classes?

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 CI 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?

    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 .

    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

    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.

    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.

    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 (ie Data). Inheritance is used to denote an "is a" relationship (ie 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".

    The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM