473,507 Members | 12,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Data implementation hiding in a class

Hi,

i have a normal class:

//test.h
class CForwardData;

class CTest
{
CForwardData *pData;

public:
//Methods to access the data.

// some other stuff...

CTest::CTest();
CTest::~CTest();

};

//test.cpp
class CForwardData
{
public:
long aLong;
double aDouble;

};

CTest::CTest()
{
pData = new CForwardData;
}

CTest::~CTest()
{
delete pData;
}

//... Methods to access the data............


This is the normal way i am using to hide the data implementation for
any user, who has access only to the *.h files.
Is there a other c++ way to do the same without using the forward
declared class ?
Thanks everyone,
Howie

Nov 4 '05 #1
4 1993
Howie wrote:
Hi,

i have a normal class:

//test.h
class CForwardData;

class CTest
{
CForwardData *pData;

public:
//Methods to access the data.

// some other stuff...

CTest::CTest();
CTest::~CTest();

};

//test.cpp
class CForwardData
{
public:
long aLong;
double aDouble;

};

CTest::CTest()
{
pData = new CForwardData;
}

CTest::~CTest()
{
delete pData;
}

//... Methods to access the data............


This is the normal way i am using to hide the data implementation for
any user, who has access only to the *.h files.
Is there a other c++ way to do the same without using the forward
declared class ?
Thanks everyone,
Howie


That looks basically the same as the Pimpl idiom, which is a standard
technique. One purpose of the idiom is to separate interface and
implementation, but the idiom should not be confused with actually
hiding the implementation from the user. See this FAQ:

http://www.parashift.com/c++-faq-lit...s.html#faq-7.6

Cheers! --M

Nov 4 '05 #2
Howie wrote:
Is there a other c++ way to do the same without using the forward
declared class ?


how about:

// test.h
class CAbstract {
public:
virtual int func() = 0;
};

class CProvider {
static CAbstract* provide();
};

// test.cpp
class CConcrete: public CAbstract {
int z;
virtual int func() { return z + 1; }
}

CAbstract* CProvider::provide() {
return new CConcrete();
}

never mind syntax errors. it's the idea that matters.

Nov 4 '05 #3

On 11/4/05 4:51 AM, in article bp********************************@4ax.com,
"Howie" <ho****@webb.de> wrote:
Hi,

i have a normal class:

//test.h
class CForwardData;

class CTest
{
CForwardData *pData;

public:
//Methods to access the data.

// some other stuff...

CTest::CTest();
CTest::~CTest();

};

//test.cpp
class CForwardData
{
public:
long aLong;
double aDouble;

};

CTest::CTest()
{
pData = new CForwardData;
}

CTest::~CTest()
{
delete pData;
}

//... Methods to access the data............


This is the normal way i am using to hide the data implementation for
any user, who has access only to the *.h files.
Is there a other c++ way to do the same without using the forward
declared class ?


It's possible to use the hidden derived class:

In CTest.h:

class CTest
{
CTest::CTest();
public:
// a factory method
static CTest * CreateCTest();

// the public API goes here

CTest::~CTest();
};
And in CTest.cpp:

class CDerivedTest : public CTest
{
public:
CDerivedTest ();
... // internal API goes here
}
CTest * CTest::CreateCTest()
{
CDerivedTest *theTest = new CDerivedTest;

return theTest;
};

In other words, every client knows that they have a pointer to a CTest
object, but none of them need to know that their CTest pointer is actually
pointing to a class derived from CTest.

For this approach to work, CTest needs a static factory method to create
CTest objects. The client cannot be allowed to create CTests directly.
Generally, a factory method is often a good idea anyway since it allows the
implementation more control over the creation of its objects.

Greg

Nov 4 '05 #4
On Fri, 04 Nov 2005 13:51:31 +0100, Howie <ho****@webb.de> wrote:
Thanks,

i will test this.

Howie
Nov 7 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
4168
by: Lorenzo Villari | last post by:
I premise I don't know C++ well but... I wondered what is this data hiding thing... I mean, if I can look at the header (and i need it beacuse of the class), then what's hidden? Can someone give...
2
3272
by: Nobody | last post by:
I took the advice from the group and got rid of the node accessor functions and arrived at this definition. I have not added the copy constructor/assignment operators to this class yet. template...
28
3381
by: Act | last post by:
Why is it suggested to not define data members as "protected"? Thanks for help!
8
4562
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member:...
3
1535
by: DaveLessnau | last post by:
In a book on Data Structures that I'm reading, the authors are describing various linked lists and trees. In these, they start with some form of node class. What's driving me crazy is that they...
12
2067
by: Alex Hunsley | last post by:
There's no really specific questions in this post, but I'm looking for people's thought on the issues within... The two main versions I've encountered for data pseudo-hiding (encapsulation) in...
2
7612
by: subramanian100in | last post by:
Is my following understanding correct ? Data abstraction means providing the interface - that is, the set of functions that can be called by the user of a class. Information hiding means...
162
10065
by: Sh4wn | last post by:
Hi, first, python is one of my fav languages, and i'll definitely keep developing with it. But, there's 1 one thing what I -really- miss: data hiding. I know member vars are private when you...
6
3932
by: Ralph | last post by:
Hi, I was reading effictive C++ and some other books again and they all tell you about hiding implementation details (proxy/pimpl/inheritance) but they never really explain when to use it. I...
0
7109
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7313
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7029
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7481
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5619
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5039
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3190
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.