How to disclose class interface but hide implementation
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: How to disclose class interface but hide implementation

Hybrid View

  1. #1
    Join Date
    Nov 2010
    Posts
    47

    How to disclose class interface but hide implementation

    I want to write a C++ class, like this:

    In MyClass.h:

    class CMyClass
    {
    public:
    CMyClass();

    int MyFun(int a, int b);

    protected:
    int m_iData;
    BOOL m_bFlag;
    }

    In MyClass.cpp

    CMyClass::CMyClass()
    {
    // Some initialization codes
    }

    int CMyClass::MyFun(int a, int b)
    {
    // Implemenation of MyFun
    }

    Now I want to let another developer to use CMyClass, but only want him to be able to:

    1. Construct a CMyClass object.
    2. Call MyFun function.

    and PREVENT him to:

    1. Know the implementation of the CMyClass object.
    2. Know the existance of the member variable m_iData and m_bFlag.

    Now I can put MyClass.h and MyClass.cpp in a static library and compile it, then send the compiled static library as well as only the MyClass.h to the developer, but he will still able to know the protected memory variables m_iData and m_bFlag, and based on them, he can guess the implementation of the MyFun. How to prevent this?

    Thanks

  2. #2
    Join Date
    Aug 2008
    Posts
    902

    Re: How to disclose class interface but hide implementation

    Quote Originally Posted by AlanCCC View Post
    1. Know the implementation of the CMyClass object.
    The implementation is already hidden, since your code is in your CPP file, not your H/HPP file.

    Quote Originally Posted by AlanCCC View Post
    2. Know the existance of the member variable m_iData and m_bFlag.
    The only way to hide the private members is to use an opaque pointer, also known as the pimpl idiom.

    Code:
    class CMyClass
    {
    public:
        CMyClass();
        int MyFun(int a, int b);
    
    private:
        class CMyClassImpl *impl; //pointer to forward declared private implementation class
    }
    Then you private data members go in your CMyClassImpl class, which is kept separate from your public headers. Inside your CMyClass implementation, you can access things like m_iData with impl->m_iData.

    See?

  3. #3
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,281

    Re: How to disclose class interface but hide implementation

    Quote Originally Posted by AlanCCC View Post
    and PREVENT him to:

    1. Know the implementation of the CMyClass object.
    2. Know the existance of the member variable m_iData and m_bFlag.
    I'd say it primarily depends on what you mean by "Know"...

    1. If you just want to be able to change implementation details without impacting people using your code.
    2. If you mean want to keep your code a company secret, and prevent the other developer from stealing your hard written code...

    1: Just use the pimpl idiom.
    2: Use the pimpl idiom, but compile it your self, and only deliver the .h of your CMyClass object. The client does not need the rest.
    Is your question related to IO?
    Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center