devxlogo

Hide Data and Implemention Details

Hide Data and Implemention Details

Consider the following class:

#include "OtherData.h"class MyClass {  OtherData m_data;public:  bool func(OtherData data);};

It seems as though this class hides its implementation details efficiently, but is actually doesn’t! The user knows about the OtherData type and its requirements in its header file. To overcome this situation, you can do this:

class OtherData; // forward refernceclass MyClass {  OtherData *m_pData;public:  bool func(const OtherData &data);};

But what if you’ve got more than one field? Suppose you have a forward reference per data type. Now, you can define another extra class and store all the data there:

class DataHolder;class MyClass {  DataHolder *m_pDataHolder;public:  void func1(const OtherData1 &data);  bool func2(const OtherData2 &data);};

Now all the data are defined in DataHolder, which is then hidden efficiently.

See also  Monetize TikTok For Your Business
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist