Connecting Tech Pros Worldwide Forums | Help | Site Map

Data implementation hiding in a class

Howie
Guest
 
Posts: n/a
#1: Nov 4 '05
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


mlimber
Guest
 
Posts: n/a
#2: Nov 4 '05

re: Data implementation hiding in a class


Howie wrote:[color=blue]
> 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[/color]

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

makc.the.great@gmail.com
Guest
 
Posts: n/a
#3: Nov 4 '05

re: Data implementation hiding in a class


Howie wrote:[color=blue]
> Is there a other c++ way to do the same without using the forward
> declared class ?[/color]

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.

Greg Herlihy
Guest
 
Posts: n/a
#4: Nov 4 '05

re: Data implementation hiding in a class





On 11/4/05 4:51 AM, in article bplmm11fsg8cigrj0krn869ov0uqkgahh0@4ax.com,
"Howie" <howieh@webb.de> wrote:
[color=blue]
> 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 ?[/color]

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

Howie
Guest
 
Posts: n/a
#5: Nov 7 '05

re: Data implementation hiding in a class


On Fri, 04 Nov 2005 13:51:31 +0100, Howie <howieh@webb.de> wrote:


Thanks,

i will test this.

Howie
Closed Thread