Featured post
c++ - How to exchange data between classes? -
i'm learning c++ , moving project c c++. in process, stumbled on problem: how save/update variables in use in several classes? in c used global variables, not c++.
so, let's assume 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 class application's main window interacts user input etc.
want do:
the question is: should create classes, make data class members available other classes. should use inheritance?
start observing possible relations between instances of 2 classes. let a instance of class , b instance of class b. if a uses b, class can have member instance of class b (b), pointer b (which of type b*), or reference of b (which of type b&). if 1 method of class uses b, have again same 3 options: b, b* or b& can method's arguments. having b* , b& class members suggests a not control b's lifetime class must have method sets these members through parameters. question of ownership (objects' lifetimes) has big role in design of relationship between classes. main relationships briefly described in article.
- Get link
- X
- Other Apps
Comments
Post a Comment