Opaque pointer
Opaque data type which stores a memory address
title: "Opaque pointer" type: doc version: 1 created: 2026-02-28 author: "Wikipedia contributors" status: active scope: public tags: ["data-types", "c_(programming_language)", "c++", "articles-with-example-c++-code", "computer-programming"] description: "Opaque data type which stores a memory address" topic_path: "general/data-types" source: "https://en.wikipedia.org/wiki/Opaque_pointer" license: "CC BY-SA 4.0" wikipedia_page_id: 0 wikipedia_revision_id: 0
::summary Opaque data type which stores a memory address ::
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.
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.{{cite web |author = Chris McKillop |title = Programming Tools — Opaque Pointers |publisher = QNX Software Systems |url = http://community.qnx.com/sf/docman/do/downloadDocument/projects.toolchain/docman.root.articles/doc1150 |access-date = 2019-01-16 |archive-date = 2021-11-12 |archive-url = https://web.archive.org/web/20211112072321/http://community.qnx.com/sf/docman/do/downloadDocument/projects.toolchain/docman.root.articles/doc1150 |url-status = dead
This technique is described in Design Patterns as the Bridge pattern. It is sometimes referred to as "handle classes", |first = Bruce |last = Eckel |author-link = Bruce Eckel |year = 2000 |title = |edition = 2nd |volume = 1: Introduction to standard C++ |chapter = Chapter 5: Hiding the implementation |publisher = Prentice Hall |isbn = 0-13-979809-9 |chapter-url= http://web.mit.edu/merolish/ticpp/Chapter05.html |url = https://archive.org/details/thinkinginc00ecke |url-access = registration the "Pimpl idiom" (for "pointer to implementation idiom"), | first = Vladimir | last = Batov | title = Making Pimpl Easy | date = 2008-01-25 | journal = Dr. Dobb's Journal | url = http://ddj.com/cpp/205918714 | access-date= 2008-05-07 "Compiler firewall idiom", |first = Herb |last = Sutter |orig-date = July-August 1998 |year = 2009 |title = The Joy of Pimpls (or, more about the compiler-firewall idiom) |magazine = |volume = 10 |issue = 7 |url = http://www.gotw.ca/publications/mill05.htm |via = gotw.ca |url-status = live |access-date = 2025-09-16 | df = dmy-all |quote = [linked to] original article substantially as first published; most current vers. in book (2000, Addison-Wesley) "d-pointer" or "Cheshire Cat", especially among the C++ community. It is heavily used in the Qt and KDE |url = https://community.kde.org/Policies/Binary_Compatibility_Issues_With_C%2B%2B#Using_a_d-Pointer |title = Policies/Binary Compatibility Issues With C++ — Using a d-Pointer |website = KDE Community Wiki |access-date = 2025-12-16
Examples
Ada
::code[lang=ada] package Library_Interface is
type Handle is limited private;
-- Operations...
private type Hidden_Implementation; -- Defined in the package body type Handle is access Hidden_Implementation; end Library_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).
::code[lang=ada]
package body Library_Interface is
type Hidden_Implementation is record ... -- The actual implementation can be anything end record;
-- Definition of the operations...
end Library_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. |title = Re: What's its name again? |author = Robert A. Duff |date = 2002-07-29 |newsgroup = comp.lang.ada |url = http://groups.google.es/group/comp.lang.ada/msg/a886bf7922727acf |url-status = dead |access-date = 2007-10-11 |archive-url = https://web.archive.org/web/20090729110825/http://groups.google.es/group/comp.lang.ada/msg/a886bf7922727acf |archive-date = 2009-07-29 | df = dmy-all
C
In : ::code[lang=c] #pragma once
typedef struct Integer Integer;
/*
- The compiler considers struct obj an incomplete type. Incomplete types
- can be used in declarations. */
size_t integerSize(void);
void integerSetValue(Integer*, int);
int integerGetValue(Integer*);
::
In : ::code[lang=c] #include "Integer.h"
typedef struct Integer { int value; } Integer;
/*
- The caller will handle allocation.
- Provide the required information only */
size_t integerSize(void) { return sizeof(Integer); }
void integerSetValue(Integer* i, int val) { i->value = val; }
int integerGetValue(Integer* i) { return i->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 : ::code[lang=cpp] export module org.example.MyClass;
import std;
using std::unique_ptr;
export namespace org::example {
class MyClass { private: struct IntPair; // Not defined here unique_ptr ptr; // Opaque pointer public: MyClass(); // Constructor MyClass(const MyClass&); // Copy constructor MyClass(MyClass&&); // Move constructor MyClass& operator=(const MyClass&); // Copy assignment operator MyClass& operator=(MyClass&&); // Move assignment operator ~MyClass(); // Destructor
// Other operations...
};
} ::
In : ::code[lang=cpp] module org.example.MyClass;
namespace org::example {
struct MyClass::IntPair { int a; int b; };
MyClass::MyClass(): ptr{std::make_unique()} {}
MyClass::MyClass(const MyClass& other): ptr{std::make_unique(*other.ptr)} {}
MyClass::MyClass(MyClass&& other) = default;
MyClass& MyClass::operator=(const MyClass& other) { *ptr = *other.ptr; return *this; }
MyClass& MyClass::operator=(MyClass&&) = default;
MyClass::~MyClass() = default;
} ::
References
::callout[type=info title="Wikipedia Source"] This article was imported from Wikipedia and is available under the Creative Commons Attribution-ShareAlike 4.0 License. Content has been adapted to SurfDoc format. Original contributors can be found on the article history page. ::