首页 > 其他分享 >解决二进制兼容的问题

解决二进制兼容的问题

时间:2022-12-08 17:55:09浏览次数:30  
标签:Widget 二进制 text Label WidgetPrivate 解决 兼容 ptr

一个动态链接到较早版本的库的程序,在不经过重新编译的情况下能够继续运行在新版本的库,这样的库即“二进制兼容

.h

 1 /*二进制兼容库
 2 * 隐藏实现细节:我们只需要发布 WidgetLib 的头文件和二进制文件,.cpp 文件可以是闭源的
 3 *头文件很干净,没有实现细节的干扰,可以作为 API 实现
 4 *由于供实现的头文件都被移动到了实现的源代码文件中,编译会更快
 5 */
 6 class WidgetPrivte;
 7 class Widget;
 8 
 9 /*为了能够在私有的实现类中访问其所在公共类,我们引入一个 Q 指针*/
10 struct WidgetPrivate
11 {
12     WidgetPrivate() {}
13     WidgetPrivate(Widget* q) : q_ptr(q) { }
14     Widget* q_ptr;
15     int     a;
16     string str;    
17 };
18 
19 class Widget
20 {
21 public:
22     Widget();
23     int getA() const;
24 
25 private:
26     WidgetPrivate* d_ptr;
27 };
28 
29 struct LabelPrivate
30 {
31     string text;
32 };
33 
34 class Label : public Widget
35 {
36 public:
37     Label();
38     string text();
39 private:
40     LabelPrivate* d_ptr;       //每个类维护自己的类指针
41 };
View Code

.cpp

 1 /*Widget*/
 2 Widget::Widget() :d_ptr(new WidgetPrivate)
 3 {
 4 
 5 }
 6 
 7 int Widget::getA() const
 8 {
 9     return d_ptr->a;
10 }
11 
12 
13 Label::Label() : d_ptr(new LabelPrivate)
14 {
15 
16 }
17 
18 string Label::text()
19 {
20     return d_ptr->text;
21 }
View Code

 

标签:Widget,二进制,text,Label,WidgetPrivate,解决,兼容,ptr
From: https://www.cnblogs.com/sansuiwantong/p/16966860.html

相关文章