Opaque pointer

本文通过C++实例展示了如何实现类的封装特性,包括构造函数、复制构造函数、移动构造函数、赋值操作符和析构函数的实现。同时,介绍了在类声明中使用d-pointer作为唯一私有数据成员的方法,该方法允许类声明隐藏除d-pointer之外的所有私有数据成员,并确保了类接口的二进制兼容性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C[edit]

/* obj.h */
 
struct obj;
 
/*
 * The compiler considers struct obj an incomplete type. Incomplete types
 * can be used in declarations.
 */
 
size_t obj_size(void);
 
int obj_setid(struct obj *, int);
 
int obj_getid(struct obj *, int *);
/* obj.c */
 
#include "obj.h"
 
struct obj {
    int id;
};
 
/*
 * The caller will handle allocation.
 * Provide the required information only
 */
 
size_t
obj_size(void)
{
    return sizeof(struct obj);
}
 
int
obj_setid(struct obj *o, int i)
{
    if (o == NULL) return -1;
    o->id = i;
    return 0;
}
 
int
obj_getid(struct obj *o, int *i)
{
    if (o == NULL || i == NULL) return -1;
    *i = o->id;
    return 0;
}

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 declaration 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 was also changed.

C++[edit]

//header file:
class Handle {
public:
    Handle();                         // Constructor
    Handle(const Handle&);            // Copy constructor
    Handle(Handle&&);                 // Move constructor
    Handle& operator=(const Handle&); // Copy assignment operator
    ~Handle();                        // Destructor
    // Other operations...
 
private:
    struct CheshireCat;               // Not defined here
    CheshireCat* smile;               // Handle
};
//CPP file:
#include "handle.h"
 
struct Handle::CheshireCat {
    int a;
    int b;
};
 
Handle::Handle()
    : smile(new CheshireCat()) {
    // do nothing
}
 
Handle::Handle(const Handle& other)
    : smile(new CheshireCat(*other.smile)) {
    // do nothing
}
 
Handle::Handle(Handle&& other) 
    : smile(0)
{
    std::swap(smile, other.smile);
}
 
Handle& Handle::operator=(const Handle &other) {
    if(this != &other) {
        *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: (a) more of the class implementation is hidden from view; (b) adding new data members to the private struct does not affect binary compatibility; (c) the header file containing the class declaration only needs to #include those other 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. The d-pointer is heavily used in the Qt and KDE libraries.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值