c++ - How to access to a variables defined and assigned by another API -



c++ - How to access to a variables defined and assigned by another API -

sorry newbie question. i'm working on c++ since few times , manage informations related device. goal name of device in variables , class access variable.

i have defined class device:

class device { public: int value; ... }

in main.cpp,

int main (...) { device mydevice; mydevice.value = 10; browse ui; ui.show(); }

in browser.cpp, able mydevice.value , display it. i'm not sure how it. browser.cpp cpp code display info supported ui in qt. current ui displayed using ui.show() api.

value should private fellow member of device, otherwise you'd exposing private info of device class clients. although code run, considered bad programming practice violates encapsulation principle. 1 time create value private info member, in order clients of device class access it, should provide getter method getvalue returns value. 1 time have place, should model browser take reference device object. in constructor of browser or accomplish through fellow member function of browser. 1 time browser knows device instance, can phone call getvalue retrieve value can display in ui. in summary, code this:

class device { public: int getvalue() const { homecoming m_value; } private: int m_value; }; class browser { public: browser(const device& device) : m_device(device) { } void displayvalue() { somewidget.show(m_device.getvalue()); } private: const device& m_device; };

i'd start beginner's book on c++ (and object oriented programming) familiarize concepts of encapsulation, reference, etc.

c++ qt

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -