Data implementation hiding in a class

H

Howie

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
 
M

mlimber

Howie said:
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-lite/classes-and-objects.html#faq-7.6

Cheers! --M
 
M

makc.the.great

Howie said:
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.
 
G

Greg Herlihy

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top