C[edit]
/* obj.h */ struct obj; /* * The compiler considers struct obj an incomplete type. Incomplete types * can be used in declarations. */ size_t obj_size(void); int obj_setid(struct obj *, int); int obj_getid(struct obj *, int *);
/* obj.c */ #include "obj.h" struct obj { int id; }; /* * The caller will handle allocation. * Provide the required information only */ size_t obj_size(void) { return sizeof(struct obj); } int obj_setid(struct obj *o, int i) { if (o == NULL) return -1; o->id = i; return 0; } int obj_getid(struct obj *o, int *i) { if (o == NULL || i == NULL) return -1; *i = o->id; return 0; }
This example demonstrates a way to achieve the information hiding (encapsulation) aspect of Object-Oriented Programming using the C language. If someone wanted to change the declaration of struct obj, it would be unnecessary to recompile any other modules in the program that use the obj.h header file unless the API was also changed.
C++[edit]
//header file: class Handle { public: Handle(); // Constructor Handle(const Handle&); // Copy constructor Handle(Handle&&); // Move constructor Handle& operator=(const Handle&); // Copy assignment operator ~Handle(); // Destructor // Other operations... private: struct CheshireCat; // Not defined here CheshireCat* smile; // Handle };
//CPP file: #include "handle.h" struct Handle::CheshireCat { int a; int b; }; Handle::Handle() : smile(new CheshireCat()) { // do nothing } Handle::Handle(const Handle& other) : smile(new CheshireCat(*other.smile)) { // do nothing } Handle::Handle(Handle&& other) : smile(0) { std::swap(smile, other.smile); } Handle& Handle::operator=(const Handle &other) { if(this != &other) { *smile = *(other.smile); } return *this; } Handle::~Handle() { delete smile; }
One type of opaque pointer commonly used in C++ class declarations is the d-pointer. The d-pointer is the only private data member of the class and points to an instance of a struct. Named by Arnt Gulbrandsen of Trolltech, this method allows class declarations to omit private data members, except for the d-pointer itself.[6] The result: (a) more of the class implementation is hidden from view; (b) adding new data members to the private struct does not affect binary compatibility; (c) the header file containing the class declaration only needs to #include those other files needed for the class interface, rather than for its implementation. One side benefit is that compilations are faster because the header file changes less often. The d-pointer is heavily used in the Qt and KDE libraries.