Opaque pointer

Last updated

In computer programming, an opaque pointer is a special case of an opaque data type, a data type declared to be a pointer to a record or data structure of some unspecified type.

Contents

Opaque pointers are present in several programming languages including Ada, C, C++, D and Modula-2.

Use

If the language the pointer is implemented with is strongly typed, programs and procedures that have no other information about an opaque pointer type T can still declare variables, arrays, and record fields of type T, assign values of that type, and compare those values for equality. However, they will not be able to de-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 the implementation details of an interface from ordinary clients, so that the implementation may be changed without the need to recompile the modules 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 providing binary code compatibility through different versions of a shared library, for example.

This technique is described in Design Patterns as the Bridge 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 the Qt [5] and KDE [6] libraries.

Examples

Ada

packageLibrary_InterfaceistypeHandleislimitedprivate;-- Operations...privatetypeHidden_Implementation;-- Defined in the package bodytypeHandleisaccessHidden_Implementation;endLibrary_Interface;

The type Handle 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 after Tucker Taft, the main designer of Ada 95 – because they were introduced in the so-called 'Taft Amendment' to Ada 83. [7]

C

In Integer.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*);

In Integer.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 the information hiding (encapsulation) aspect of object-oriented programming using the C language. If someone wanted to change the definition of struct Integer, it would be unnecessary to recompile any other modules in the program that use the Integer.h header file unless the API was also changed. Note that it may be desirable for the functions to check that the passed pointer is not NULL, but such checks have been omitted above for brevity.

C++

In MyClass.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...};}

In MyClass.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;}

See also

References

  1. Chris McKillop. "Programming Tools Opaque Pointers". QNX Software Systems. Archived from the original on 2021-11-12. Retrieved 2019-01-16.
  2. 1 2 Eckel, Bruce (2000). "Chapter 5: Hiding the implementation". Thinking in C++ . Vol. 1: Introduction to standard C++ (2nd ed.). Prentice Hall. ISBN   0-13-979809-9.
  3. Batov, Vladimir (2008-01-25). "Making Pimpl Easy". Dr. Dobb's Journal . Retrieved 2008-05-07.
  4. Sutter, Herb (2009) [July-August 1998]. "The Joy of Pimpls (or, more about the compiler-firewall idiom)". C++ Report . Vol. 10, no. 7. Retrieved 16 September 2025 via gotw.ca. [linked to] original article substantially as first published; most current vers. in book Exceptional C++ (2000, Addison-Wesley){{cite magazine}}: CS1 maint: url-status (link)
  5. "d-Pointer". Qt wiki. Retrieved 23 Dec 2016.
  6. "Policies/Binary Compatibility Issues With C++ — Using a d-Pointer". KDE Community Wiki. Retrieved 2025-12-16.
  7. Robert A. Duff (29 July 2002). "Re: What's its name again?". Newsgroup:  comp.lang.ada. Archived from the original on 29 July 2009. Retrieved 11 October 2007.