HOME

TheInfoList



OR:

In
computer programming Computer programming or coding is the composition of sequences of instructions, called computer program, programs, that computers can follow to perform tasks. It involves designing and implementing algorithms, step-by-step specifications of proc ...
, an opaque pointer is a special case of an opaque data type, a
data type In computer science and computer programming, a data type (or simply type) is a collection or grouping of data values, usually specified by a set of possible values, a set of allowed operations on these values, and/or a representation of these ...
declared to be a pointer to a record or
data structure In computer science, a data structure is a data organization and storage format that is usually chosen for Efficiency, efficient Data access, access to data. More precisely, a data structure is a collection of data values, the relationships amo ...
of some unspecified type. Opaque pointers are present in several
programming language A programming language is a system of notation for writing computer programs. Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
s 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 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, execution of a plan, idea, scientific modelling, model, design, specification, Standardization, standard, algorithm, policy, or the Management, administration or management of a process or Goal ...
details of an interface from ordinary clients, so that the
implementation Implementation is the realization of an application, execution of a plan, idea, scientific modelling, model, design, specification, Standardization, standard, algorithm, policy, or the Management, administration or management of a process or Goal ...
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. This is important for providing binary code compatibility through different versions of a
shared library In computing, a library is a collection of System resource, resources that can be leveraged during software development to implement a computer program. Commonly, a library consists of executable code such as compiled function (computer scienc ...
, for example. This technique is described in '' Design Patterns'' as the Bridge pattern. It is sometimes referred to as "
handle A handle is a part of, or an attachment to, an object that allows it to be grasped and object manipulation, manipulated by hand. The design of each type of handle involves substantial ergonomics, ergonomic issues, even where these are dealt wi ...
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, 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''. Objects can contain data (called fields, attributes or properties) and have actions they can perform (called procedures or methods and impl ...
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 connection between computers or between computer programs. It is a type of software interface, offering a service to other pieces of software. A document or standard that describes how to build ...
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 * 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 enable collaborative work on its projects. Its products include the KDE Plasma gra ...
libraries.


See also

*
Application binary interface An application binary interface (ABI) is an interface exposed by software that is defined for in-process machine code access. Often, the exposing software is a library, and the consumer is a program. An ABI is at a relatively low-level of a ...
*
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


References


External links


The Pimpl idiom


o
Compilation Firewalls



D-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_(programming_language) C++ Articles with example C++ code Computer programming