hiding private member in header file

This is a discussion on hiding private member in header file within the C++ Programming forums, part of the General Programming Boards category; I develop my own string class. I have two files String.cpp and String.h. I can make static library and give ...

  1. #1
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712

    hiding private member in header file

    I develop my own string class. I have two files String.cpp and String.h. I can make static library and give others only header file and .lib file to hide my source code.
    But in .h file one can see what my class is consisting of and simply change private to public access. So that private access specificator is just compiler trick. How can I expose only public methods? How this can be achived? I'm using VC++ .Net and like to see some code example or to get some usefull link to follow.
    Thanks very much.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    You can use the Pimpl (Pointer to implementation) idiom:
    Code:
    // Header file
    #ifndef MICKOSTRING_H_
    #define MICKOSTRING_H_
    
    class String {
    public:
      String();
      ~String();
      // Interface
    private:
      struct StringImpl *pimpl;
    };
    
    #endif
    Code:
    // Implementation file
    #include "mickostring.h"
    
    struct StringImpl {
    public:
      StringImpl();
      ~StringImpl();
    public:
      // Private members of String
    };
    
    String::String()
      : pimpl ( new StringImpl )
    {}
    
    String::~String()
    {
      delete pimpl;
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. #include header files or .cpp files?
    By DoctorX in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2006, 12:21 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  3. Need help with calculator program
    By Kate in forum C# Programming
    Replies: 1
    Last Post: 01-16-2004, 10:48 AM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21