Incomputer programming, anopaque pointer is a special case of anopaque data type, adata type declared to be apointer to arecord ordata structure of some unspecified type.
Opaque pointers are present in severalprogramming languages includingAda,C,C++,D andModula-2.
If the language the pointer is implemented with isstrongly typed,programs andprocedures that have no other information about an opaque pointer typeT can still declarevariables,arrays, and record fields of typeT, assign values of that type, and compare those values for equality. However, they will not be able tode-reference such a pointer, and can only change the object's content by calling some procedure that has the missing information.
Opaque pointers are a way to hide theimplementation details of aninterface from ordinary clients, so that theimplementation may be changed without the need to recompile themodules using it. This benefits the programmer as well since a simple interface can be created, and most details can be hidden in another file.[1] This is important for providingbinary code compatibility through different versions of ashared library, for example.
This technique is described inDesign Patterns as theBridge pattern. It is sometimes referred to as "handle classes",[2] the "Pimpl idiom" (for "pointer to implementation idiom"),[3] "Compiler firewall idiom",[4] "d-pointer" or "Cheshire Cat", especially among the C++ community.[2] It is heavily used in theQt[5] andKDE[6] libraries.
packageLibrary_InterfaceistypeHandleislimitedprivate;-- Operations...privatetypeHidden_Implementation;-- Defined in the package bodytypeHandleisaccessHidden_Implementation;endLibrary_Interface;
The typeHandle is an opaque pointer to the real implementation, that is not defined in the specification. Note that the type is not only private (to forbid the clients from accessing the type directly, and only through the operations), but also limited (to avoid the copy of the data structure, and thus preventing dangling references).
packagebodyLibrary_InterfaceistypeHidden_Implementationisrecord...-- The actual implementation can be anythingend record;-- Definition of the operations...endLibrary_Interface;
These types are sometimes called "Taft types" – named afterTucker Taft, the main designer of Ada 95 – because they were introduced in the so-called 'Taft Amendment' to Ada 83.[7]
InInteger.h:
#pragma oncetypedefstructIntegerInteger;/* * The compiler considers struct obj an incomplete type. Incomplete types * can be used in declarations. */size_tintegerSize(void);voidintegerSetValue(Integer*,int);intintegerGetValue(Integer*);
InInteger.c:
#include"Integer.h"typedefstructInteger{intvalue;}Integer;/* * The caller will handle allocation. * Provide the required information only */size_tintegerSize(void){returnsizeof(Integer);}voidintegerSetValue(Integer*i,intval){i->value=val;}intintegerGetValue(Integer*i){returni->value;}
This example demonstrates a way to achieve theinformation hiding (encapsulation) aspect ofobject-oriented programming using the C language. If someone wanted to change the definition ofstruct Integer, it would be unnecessary to recompile any other modules in the program that use theInteger.h header file unless theAPI was also changed. Note that it may be desirable for the functions to check that the passed pointer is notNULL, but such checks have been omitted above for brevity.
InMyClass.cppm:
exportmoduleorg.example.MyClass;importstd;usingstd::unique_ptr;exportnamespaceorg::example{classMyClass{private:structIntPair;// Not defined hereunique_ptr<IntPair>ptr;// Opaque pointerpublic:MyClass();// ConstructorMyClass(constMyClass&);// Copy constructorMyClass(MyClass&&);// Move constructorMyClass&operator=(constMyClass&);// Copy assignment operatorMyClass&operator=(MyClass&&);// Move assignment operator~MyClass();// Destructor// Other operations...};}
InMyClass.cpp:
moduleorg.example.MyClass;namespaceorg::example{structMyClass::IntPair{inta;intb;};MyClass::MyClass():ptr{std::make_unique<IntPair>()}{}MyClass::MyClass(constMyClass&other):ptr{std::make_unique<MyClass>(*other.ptr)}{}MyClass::MyClass(MyClass&&other)=default;MyClass&MyClass::operator=(constMyClass&other){*ptr=*other.ptr;return*this;}MyClass&MyClass::operator=(MyClass&&)=default;MyClass::~MyClass()=default;}
[linked to] original article substantially as first published; most current vers. in bookExceptional C++ (2000, Addison-Wesley)
{{cite magazine}}: CS1 maint: url-status (link)