Re: Splitting up the definitions of a class into different files (splitting public from private)?

Discussion in 'C++' started by John Ericson, Jul 19, 2003.

  1. John Ericson

    John Ericson Guest

    > "Mark" <> wrote in message
    news:_vdSa.99451$H17.29351@sccrnsc02...
    > Is it possible in C++ to somehow split the public and

    private class member definitions of a class such that the
    "public" .h file has just the public definitions?
    >
    > For example, something along these lines....
    >
    > In file MyClass.h:
    >
    > class MyClass {
    >
    > public:
    > MyClass();
    > ~MyClass();
    >
    > int getAttribute();
    > }
    >
    > and in file MyClass.cpp
    >
    > class MyClass {
    >
    > private:
    > int _attribute;
    > }
    >
    > MyClass::MyClass() {}
    > MyClass::~MyClass() {}
    >
    > int MyClass::getAttribute() { return _attribute; }
    >
    >
    > I'm trying to find a way to hide away all the internal

    ("private:") stuff from anybody which includes MyClass.h, as
    well as to optimize compile times by not having modules
    which include MyClass.h have to compile other header files
    which are only required by the private members of a class.
    >
    > Thanks,
    > Mark
    >


    Sure, the "Cheshire Cat" pattern will do what you want (also
    called "Bridge" or "Handle/Body").

    class MyClassImp; // Forward declaration.

    class MyClass{
    public:
    // ctors declarations etc.
    int getAttribute();
    private:
    MyClassImp* imp_;
    };

    - -
    Best Regards, John E.
    John Ericson, Jul 19, 2003
    #1
    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.

Share This Page