Question: in microsoft visual c++  , how can i hide private members of a class in a static link library? i wrote a static link library in two files : code.cpp and header.h , header.h includes the interface of a class named Class1 , which have private members and public members , and code.cpp includes the implementation of that class , i dont want the programmer who uses the static link library to see the private members decleration of the class (Class1) , if i give header.h to the programmer he will know the declaration of the private members , and if i exclude the private members of his copy of header.h , it class (Class1) will not work.

Answer: One way to do it is to make Class1 call some other class for all actual implementation. So you make a new class, Class1Impl, which does all the real work. Class1 has a pointer to Class1Impl, and all of the member functions of Class1 call the appropriate member function of Class1Impl. You don't need to give out the header for Class1Impl as it can just be predeclared at the top of the Class1 declaration. \nEx:\n//class1.h\nclass Class1Impl;\n\nclass Class1\n{\nprivate:\nClass1Impl* m_pImpl;\n\npublic:\n  Class1();\n  void DoStuff();\n};\n\n//Class1.cpp\n#include "Class1.h"\n#include "Class1Impl.h"\n\nClass1::Class1()\n{\n  m_pImpl = new Class1Impl;\n}\n\nvoid Class1::DoStuff()\n{\n  m_pImpl->DoStuff();\n}\n\n//class1impl.h\n//hidden, never give this source out\nclass Class1Impl\n{\nprivate:\n  long m_lHiddenVal;\npublic:\n  void DoStuff() { printf("Woo! %d", m_lHiddenVal); }\n};
This question from the Yahoo Q&A forum is categorized in the topic: Society & Culture