GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing en.wikipedia.org — Opaque pointer - Wikipedia In computer programming, an opaque pointer is a special case of an opaque data type, a data type 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. UseIf the language the pointer is implemented with is strongly typed, programs and procedures that have no other information about an opaque pointer type 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] It is heavily used in the Qt[5] and KDE[6] libraries. ExamplesAdapackage 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 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.[7] CIn Integer.h: #pragma once typedef struct Integer Integer; /* * The compiler considers struct Integer an incomplete type. Incomplete types * can be used in declarations. */ size_t integerSize(); void integerSetValue(Integer* i, int val); int integerGetValue(Integer* i); In Integer.c: #include "Integer.h" typedef struct Integer { int value; } Integer; /* * The caller will handle allocation. * Provide the required information only */ size_t integerSize() { return sizeof(Integer); } void integerSetValue(Integer* i, int val) { i->value = val; } int integerGetValue(Integer* i) { return i->value; } 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 definition of C++In C++, the usage of modules can be used to provide the same compilation speed benefits as the PIMPL idiom, however the PIMPL idiom remains useful for ABI size guarantees and providing encapsulation. In Foo.cppm: export module wikipedia.examples:Foo; import std; using std::unique_ptr; export namespace wikipedia::examples { class Foo { private: struct IntPair; // Not defined here unique_ptr<IntPair> ptr; // Opaque pointer public: Foo(); // Constructor Foo(const Foo& other); // Copy constructor Foo(Foo&& other); // Move constructor Foo& operator=(const Foo& other); // Copy assignment operator Foo& operator=(Foo&& other); // Move assignment operator ~Foo(); // Destructor // Other operations... }; } In Foo.cpp: module wikipedia.examples:Foo; namespace wikipedia::examples { struct Foo::IntPair { int a; int b; }; Foo::Foo(): ptr{std::make_unique<IntPair>()} {} Foo::Foo(const Foo& other): ptr{std::make_unique<IntPair>(*other.ptr)} {} Foo::Foo(Foo&& other) = default; Foo& Foo::operator=(const Foo& other) { *ptr = *other.ptr; return *this; } Foo& Foo::operator=(Foo&& other) = default; Foo::~Foo() = default; } See alsoReferences
External linksThe Wikibook Ada Programming has a page on the topic of: Taft types The Wikibook C++ Programming has a page on the topic of: the Pointer To Implementation (pImpl) idiom
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |