Opentopia Directory Encyclopedia Tools

Opaque pointer

Encyclopedia : O : OP : OPA : Opaque pointer



 

In computer programming, an opaque pointer is a datatype that hides its internal implementation using a pointer. This allows the implementation of the whole interface to change without the need to recompile the modules using it. This is important for providing binary compatibility through different versions of a shared library, for example.

Examples

Opaque pointers are present in programming languages like Ada, C/C++ or Modula-2. For example, the following Ada code sample declares an opaque pointer:

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;

This technique is sometimes referred as "handle classes" or "Cheshire Cat", especially among the C++ community .

class Handle ;
#include "handle.hpp"

struct Handle::CheshireCat ;

Handle::Handle()

Handle::~Handle()

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 defined in the class's implementation file. Developed by Arnt Gulbrandsen of Trolltech, this method allows class declarations to omit private data members, except for the d-pointer itself. The result is that more of the class's implementation is hidden from view and adding new data members to the private struct does not affect binary compatibility. The d-pointer is heavily used in the Qt and KDE libraries.

See also

References

External links

[[wikibooks:Ada Programming|Wikibooks Ada Programming]] has more about this subject:
[[wikibooks:Ada Programming/Tips#Full declaration of a type can be deferred to the unit's body|Taft types]]

 


From Wikipedia, the Free Encyclopedia. Original article here. Support Wikipedia by contributing or donating.
All text is available under the terms of the GNU Free Documentation License See Wikipedia Copyrights for details.


Search Titles
0123456789
ABCDEFGHIJ
KLMNOPQRST
UVWXYZ?

E-mail this article to:

Personal Message: