World Library  
Flag as Inappropriate
Email this Article

Opaque pointer

Article Id: WHEBN0002570200
Reproduction Date:

Title: Opaque pointer  
Author: World Heritage Encyclopedia
Language: English
Subject: Application binary interface, Opaque data type, Identifier, Handle (disambiguation), Programming idiom
Collection: Articles with Example C++ Code, C++, Data Types
Publisher: World Heritage Encyclopedia

Opaque pointer

In computer programming, an opaque pointer is a special case of an opaque data type, a datatype 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.

If the language 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]


  • Examples 1
    • Ada 1.1
    • C 1.2
    • C++ 1.3
    • C# 1.4
  • See also 2
  • References 3
  • External links 4



package Library_Interface is

   type Handle is limited private;

   -- Operations...

   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).

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.[5]


/* 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

    return sizeof(struct obj);

obj_setid(struct obj *o, int i)
    if (o == NULL) return -1;
    o->id = i;
    return 0;

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.


//header file:
class PublicClass {
    PublicClass();                              // Constructor
    PublicClass(const PublicClass&);            // Copy constructor
    PublicClass(PublicClass&&);                 // Move constructor
    PublicClass& operator=(const PublicClass&); // Copy assignment operator
    ~PublicClass();                             // Destructor
    // Other operations...

    struct CheshireCat;                         // Not defined here
    CheshireCat* d_ptr;                         // opaque pointer
//CPP file:
#include "PublicClass.h"

struct PublicClass::CheshireCat {
    int a;
    int b;

PublicClass:: PublicClass()
    : d_ptr(new CheshireCat()) {
    // do nothing

PublicClass:: PublicClass(const PublicClass& other)
    : d_ptr(new CheshireCat(*other.d_ptr)) {
    // do nothing

PublicClass:: PublicClass(PublicClass&& other) 
    : d_ptr(other.d_ptr) {
    other.d_ptr = nullptr;

PublicClass& PublicClass::operator=(const PublicClass &other) {
    *d_ptr= *other.d_ptr;
    return *this;

PublicClass::~ PublicClass() {
    delete d_ptr;

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.


See Private class data pattern

See also


  1. ^ Chris McKillop. "Programming Tools — Opaque Pointers". QNX Software Systems. Retrieved 2005-08-29. 
  2. ^ a b  
  3. ^ Vladimir Batov (2008-01-25). "Making Pimpl Easy".  
  4. ^ Herb Sutter. The Joy of Pimpls (or, More About the Compiler-Firewall Idiom)
  5. ^ Robert A. Duff (2002-07-29). "Re: What's its name again?".  
  6. ^ Using a d-Pointer — Why and how KDE implements opaque pointers

External links

  • The Pimpl idiom
  • Compilation Firewalls
  • The Fast Pimpl Idiom
  • D-Pointers — KDE TechBase
  • When you "XOR the pointer with a random number"[1][2], the result is a "really opaque" pointer [3].
  • Making Pimpl Easy, Vladimir Batov
This article was sourced from Creative Commons Attribution-ShareAlike License; additional terms may apply. World Heritage Encyclopedia content is assembled from numerous content providers, Open Access Publishing, and in compliance with The Fair Access to Science and Technology Research Act (FASTR), Wikimedia Foundation, Inc., Public Library of Science, The Encyclopedia of Life, Open Book Publishers (OBP), PubMed, U.S. National Library of Medicine, National Center for Biotechnology Information, U.S. National Library of Medicine, National Institutes of Health (NIH), U.S. Department of Health & Human Services, and, which sources content from all federal, state, local, tribal, and territorial government publication portals (.gov, .mil, .edu). Funding for and content contributors is made possible from the U.S. Congress, E-Government Act of 2002.
Crowd sourced content that is contributed to World Heritage Encyclopedia is peer reviewed and edited by our editorial staff to ensure quality scholarly research articles.
By using this site, you agree to the Terms of Use and Privacy Policy. World Heritage Encyclopedia™ is a registered trademark of the World Public Library Association, a non-profit organization.

Copyright © World Library Foundation. All rights reserved. eBooks from Project Gutenberg are sponsored by the World Library Foundation,
a 501c(4) Member's Support Non-Profit Organization, and is NOT affiliated with any governmental agency or department.