Opaque pointer

Opaque pointer

From Wikipedia, the free encyclopedia

In computer programming, an opaque pointer is a special case of opaque data type, a datatype that is declared to be a pointer to a record or data structure of some unspecified type.

Opaque pointers are present in several programming languages including AdaCC++ and Modula-2.

If the language is strongly typedprograms and procedures that have no other information about an opaque pointer type T can still declare variablesarrays, 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 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 sometimes referred to as "handle classes",[2] the "Pimpl idiom" (for "pointer to implementation idiom"),[3] "Compiler firewall idiom"[4] or "Cheshire Cat", especially among the C++ community.[2]


[edit][edit]
Examples


[edit]C++

In the example below, the copy assignment operator takes its argument by value, eliminating the need to explicitly create a copy of the other object.
//header file:
class Handle {
private:
    struct CheshireCat;            // Not defined here
    CheshireCat *smile;            // Handle
 
public:
    Handle();                      // Constructor
    Handle(const Handle&);         // Copy constructor
    const Handle& operator=(Handle);     // Copy assignment operator
    ~Handle();                     // Destructor
    // Other operations...
};
//CPP file:
#include "handle.h"
 
struct Handle::CheshireCat {
    // The actual implementation can be anything
    // ...
};
 
Handle::Handle()
    : smile(new CheshireCat()) {
    // do nothing
}
 
Handle::Handle(const Handle& other)
    : smile(new CheshireCat(*(other.smile))) {
    // do nothing
}
 
const Handle &Handle::operator=(Handle other) {
    std::swap(this->smile, other.smile);
    return *this;
}
 
Handle::~Handle() {
    delete smile;
}

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. Named by Arnt Gulbrandsen of Trolltech, this method allows class declarations to omit private data members, except for the d-pointer itself.[6] The result is that more of the class' implementation is hidden from view, that adding new data members to the private struct does not affect binary compatibility, and that the header file containing the class declaration only has to #include those other files that are needed for the class interface, rather than for its implementation. As a side benefit, compiles are faster because the header file changes less often. The d-pointer is heavily used in the Qt and KDE libraries.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值