In
computer programming
Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as ana ...
, an opaque pointer is a special case of an
opaque data type
In computer science, an opaque data type is a data type whose concrete data structure is not defined in an interface. This enforces information hiding, since its values can only be manipulated by calling subroutines that have access to the missing ...
, a
data type
In computer science and computer programming, a data type (or simply type) is a set of possible values and a set of allowed operations on it. A data type tells the compiler or interpreter how the programmer intends to use the data. Most progra ...
declared to be a
pointer to a
record or
data structure
In computer science, a data structure is a data organization, management, and storage format that is usually chosen for efficient access to data. More precisely, a data structure is a collection of data values, the relationships among them, a ...
of some unspecified type.
Opaque pointers are present in several
programming language
A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language.
The description of a programming ...
s including
Ada
Ada may refer to:
Places
Africa
* Ada Foah, a town in Ghana
* Ada (Ghana parliament constituency)
* Ada, Osun, a town in Nigeria
Asia
* Ada, Urmia, a village in West Azerbaijan Province, Iran
* Ada, Karaman, a village in Karaman Province, ...
,
C,
C++
C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
,
D and
Modula-2
Modula-2 is a structured, procedural programming language developed between 1977 and 1985/8 by Niklaus Wirth at ETH Zurich. It was created as the language for the operating system and application software of the Lilith personal workstation. It w ...
.
If the language is
strongly typed,
program
Program, programme, programmer, or programming may refer to:
Business and management
* Program management, the process of managing several related projects
* Time management
* Program, a part of planning
Arts and entertainment Audio
* Progra ...
s and
procedures
Procedure may refer to:
* Medical procedure
* Instructions or recipes, a set of commands that show how to achieve some result, such as to prepare or make something
* Procedure (business), specifying parts of a business process
* Standard opera ...
that have no other information about an opaque pointer type ''T'' can still declare
variable
Variable may refer to:
* Variable (computer science), a symbolic name associated with a value and whose associated value may be changed
* Variable (mathematics), a symbol that represents a quantity in a mathematical expression, as used in many ...
s,
arrays
An array is a systematic arrangement of similar objects, usually in rows and columns.
Things called an array include:
{{TOC right
Music
* In twelve-tone and serial composition, the presentation of simultaneous twelve-tone sets such that the ...
, 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
Implementation is the realization of an application, or execution of a plan, idea, model, design, specification, standard, algorithm, or policy.
Industry-specific definitions
Computer science
In computer science, an implementation is a real ...
details of an
interface
Interface or interfacing may refer to:
Academic journals
* ''Interface'' (journal), by the Electrochemical Society
* '' Interface, Journal of Applied Linguistics'', now merged with ''ITL International Journal of Applied Linguistics''
* '' Int ...
from ordinary clients, so that the
implementation
Implementation is the realization of an application, or execution of a plan, idea, model, design, specification, standard, algorithm, or policy.
Industry-specific definitions
Computer science
In computer science, an implementation is a real ...
may be changed without the need to recompile the
module
Module, modular and modularity may refer to the concept of modularity. They may also refer to:
Computing and engineering
* Modular design, the engineering discipline of designing complex devices using separately designed sub-components
* Mo ...
s using it. This benefits the programmer as well since a simple interface can be created, and most details can be hidden in another file. This is important for providing
binary code compatibility
Binary-code compatibility (binary compatible or object-code-compatible) is a property of a computer system, meaning that it can run the same executable code, typically machine code for a general-purpose computer CPU, that another computer syste ...
through different versions of a
shared library
In computer science, a library is a collection of non-volatile resources used by computer programs, often for software development. These may include configuration data, documentation, help data, message templates, pre-written code and subro ...
, for example.
This technique is described in ''
Design Patterns
''Design Patterns: Elements of Reusable Object-Oriented Software'' (1994) is a software engineering book describing software design patterns. The book was written by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, with a foreword ...
'' as the
Bridge pattern. It is sometimes referred to as "
handle
A handle is a part of, or attachment to, an object that allows it to be grasped and manipulated by hand. The design of each type of handle involves substantial ergonomic issues, even where these are dealt with intuitively or by following tra ...
classes",
the "Pimpl idiom" (for "pointer to implementation idiom"), "Compiler firewall idiom", "d-pointer" or "Cheshire Cat", especially among the C++ community.
Examples
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).
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
Tucker may refer to:
Places United States
* Tucker, Arkansas
* Tucker, Georgia
* Tucker, Mississippi
* Tucker, Missouri
* Tucker, Utah, ghost town
* Tucker County, West Virginia
Outer space
* Tucker (crater), a small lunar impact crater in t ...
, the main designer of Ada 95—because they were introduced in the so-called Taft Amendment to Ada 83.
C
/* obj.h */
struct obj;
/*
* The compiler considers struct obj an incomplete type. Incomplete types
* can be used in declarations.
*/
size_t obj_size(void);
void obj_setid(struct obj *, int);
int obj_getid(struct obj *);
/* obj.c */
#include "obj.h"
struct obj ;
/*
* The caller will handle allocation.
* Provide the required information only
*/
size_t obj_size(void)
void obj_setid(struct obj *o, int i)
int obj_getid(struct obj *o)
This example demonstrates a way to achieve the
information hiding
In computer science, information hiding is the principle of segregation of the ''design decisions'' in a computer program that are most likely to change, thus protecting other parts of the program from extensive modification if the design decisio ...
(
encapsulation) aspect of
object-oriented programming
Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of pr ...
using the C language. If someone wanted to change the definition 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
An application programming interface (API) is a way for two or more computer programs to communicate with each other. It is a type of software interface, offering a service to other pieces of software. A document or standard that describes how ...
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++
/* PublicClass.h */
#include
class PublicClass ;
/* PublicClass.cpp */
#include "PublicClass.h"
struct PublicClass::CheshireCat ;
PublicClass::PublicClass()
: d_ptr_(std::make_unique())
PublicClass::PublicClass(const PublicClass& other)
: d_ptr_(std::make_unique(*other.d_ptr_))
PublicClass::PublicClass(PublicClass&& other) = default;
PublicClass& PublicClass::operator=(const PublicClass &other)
PublicClass& PublicClass::operator=(PublicClass&&) = default;
PublicClass::~PublicClass() = default;
The d-pointer pattern is one of the implementations of the . It is commonly used in C++ classes due to its advantages (noted below). A d-pointer is a private data member of the class that points to an instance of a structure. This method allows class declarations to omit private data members, except for the d-pointer itself. As a result,
* more of the class implementation is hidden
* adding new data members to the private structure does not affect
binary compatibility
Binary-code compatibility (binary compatible or object-code-compatible) is a property of a computer system, meaning that it can run the same executable code, typically machine code for a general-purpose computer CPU, that another computer syste ...
* the header file containing the class declaration only needs to include those 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. Note, possible disadvantage of d-pointer pattern is indirect member access through pointer (e.g., pointer to object in dynamic storage), which is sometimes slower than access to a plain, non-pointer member. The d-pointer is heavily used in the
Qt and
KDE
KDE is an international free software community that develops free and open-source software. As a central development hub, it provides tools and resources that allow collaborative work on this kind of software. Well-known products include the ...
libraries.
See also
*
Application binary interface
In computer software, an application binary interface (ABI) is an interface between two binary program modules. Often, one of these modules is a library or operating system facility, and the other is a program that is being run by a user.
An ' ...
*
Handle (computing)
In computer programming, a handle is an abstract reference to a resource that is used when application software references blocks of memory or objects that are managed by another system like a database or an operating system.
A resource handle ...
*
Programming idiom
In computer programming, a programming idiom or code idiom is a group of code fragments sharing an equivalent semantic role, which recurs frequently across software projects often expressing a special feature of a recurring construct in one or ...
References
External links
The Pimpl idiomo
Compilation FirewallsD-Pointers— KDE TechBase
* When you "XOR the pointer with a random numbe
http://udrepper.livejournal.com/13393.html], the result is a "really opaque" pointe
Making Pimpl Easy Vladimir Batov
{{Application binary interface
Data types
C++
Articles with example C++ code
Computer programming