Data implementation hiding in a class

Discussion in 'C++' started by Howie, Nov 4, 2005.

  1. Howie

    Howie Guest

    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
    Howie, Nov 4, 2005
    #1
    1. Advertising

  2. Howie

    mlimber Guest

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

    Cheers! --M
    mlimber, Nov 4, 2005
    #2
    1. Advertising

  3. Howie

    Guest

    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, 2005
    #3
  4. Howie

    Greg Herlihy Guest

    On 11/4/05 4:51 AM, in article ,
    "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 ?


    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
    Greg Herlihy, Nov 4, 2005
    #4
  5. Howie

    Howie Guest

    On Fri, 04 Nov 2005 13:51:31 +0100, Howie <> wrote:


    Thanks,

    i will test this.

    Howie
    Howie, Nov 7, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy Jefferies

    Struts/Tiles: hiding implementation

    Andy Jefferies, Mar 17, 2005, in forum: Java
    Replies:
    0
    Views:
    461
    Andy Jefferies
    Mar 17, 2005
  2. er
    Replies:
    1
    Views:
    337
  3. Replies:
    6
    Views:
    467
    benben
    Oct 29, 2007
  4. Royan
    Replies:
    9
    Views:
    269
    thufir
    Apr 16, 2008
  5. Ste
    Replies:
    41
    Views:
    808
    Thomas 'PointedEars' Lahn
    Aug 1, 2007
Loading...

Share This Page