Opaque pointer
Wikipedia, the free encyclopedia - Cite This SourceIn 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.
This technique is sometimes referred as "handle classes", the "Pimpl idiom" (for "pointer implementation idiom"), "Compiler firewall idiom" or "Cheshire Cat", especially among the C++ community.
Examples
Opaque pointers are present in several programming languages, for example Ada, C/C++ or Modula-2.
Ada
type Handle is limited private ;
-- Operations...
private
type Hidden_Implementation; -- Defined in the package body
type Handle is access Hidden_Implementation;end Library_Interface;
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).
type Hidden_Implementation is record
... -- The actual implementation can be anything
end record;
-- Definition of the operations...
end Library_Interface;
C++
struct CheshireCat; // Not defined here
CheshireCat *smile; // Handle
public:
Handle(); // Constructor
~Handle(); // Destructor
// Other operations...};
- include "handle.h"
struct Handle::CheshireCat {
... // The actual implementation can be anything};
Handle::Handle() {
smile = new CheshireCat;}
Handle::~Handle() {
delete smile;}
C
* Even though the compiler doesn't know anything about the struct
* at this point, it can use a pointer to that struct.
*/
/*
* This function creates a cat object with the given smile value.
* A result of NULL indicates an error.
*/cat_handle cat_Create(int smile);
/*
* This function destroys a cat object.
* If called with NULL, no action is taken.
*/void cat_Destroy(cat_handle cat);
/*
* This function sets the smile value of a cat object to the
* new value, and returns the old value.
* A result of -1 indicates an error.
*/int cat_Smile(cat_handle cat, int newsmile);
- include "cat.h"
- include
struct cat_t {
int smile;};
/* Create a cat object */ cat_handle cat_Create(int smile) {
cat_handle result = malloc(sizeof(struct cat_t));
if (result)
{
result->smile = smile;}
return result;}
/* Destroy a cat object */ void cat_Destroy(cat_handle cat) {
free(cat);}
/* Set the smile value of a cat */ int cat_Smile(cat_handle cat, int newsmile) {
int result = -1;
if (cat)
{
result = cat->smile;
cat->smile = newsmile;}
return result;}
See also
References
External links
- The Pimpl idiom
- D-Pointers
- When you "XOR the pointer with a random number"
, the result is a "really opaque" pointer 
Wikipedia, the free encyclopedia © 2001-2006 Wikipedia contributors (Disclaimer)
This article is licensed under the GNU Free Documentation License.
Last updated on Monday March 10, 2008 at 16:25:37 PDT (GMT -0700)
View this article at Wikipedia.org - Edit this article at Wikipedia.org - Donate to the Wikimedia Foundation