By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,515 Members | 1,772 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,515 IT Pros & Developers. It's quick & easy.

Data implementation hiding in a class

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.