How to exchange data between classes?

I am learning C ++ and moving my project from C to C ++. In this process, I came across this problem: how to save / update variables that are used in several classes? In C, I used global variables, but this is not good for C ++.

So, suppose 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 the class for the main application window, where it interacts with user input, etc.
I want to do:

create an instance of the Data class in Main_Window use Load_Data to load data from a file and save it to Data li> use the calculation class to do something with the read data in the data class

: , Data . ?

+5
5

. , a - A, b - B. a b > , A - B (b) b ( B *), b ( B &). A b, : B, B * B & . B * B &, , a b, A , . ( ) . .

+2

, Main_Window, .

class Main_Window
{
 private:
 DataObject windowData;

 public:
 void loadData(string fileName);
 void calculate();
}

loadData this->windowData. , , ++

+1

, (const) . do_calculation() Data , &. , .

+1

, OO. C ++. , . , .

SOLID. . TDD .

0

, . , (Window, Calculator ..), - (.. ). " a" ( , A B, A "" B).

, "". Data .

Data? Data, Boost shared_ptr, , "".

0
source

All Articles