update
This commit is contained in:
BIN
codingStyleIdioms/5_pImpl/.a.cpp.un~
Normal file
BIN
codingStyleIdioms/5_pImpl/.a.cpp.un~
Normal file
Binary file not shown.
BIN
codingStyleIdioms/5_pImpl/.pimplTime.cpp.un~
Normal file
BIN
codingStyleIdioms/5_pImpl/.pimplTime.cpp.un~
Normal file
Binary file not shown.
BIN
codingStyleIdioms/5_pImpl/.timePimpl.cpp.un~
Normal file
BIN
codingStyleIdioms/5_pImpl/.timePimpl.cpp.un~
Normal file
Binary file not shown.
57
codingStyleIdioms/5_pImpl/README.md
Normal file
57
codingStyleIdioms/5_pImpl/README.md
Normal file
@@ -0,0 +1,57 @@
|
||||
# C++惯用法之pImpl
|
||||
|
||||
“指向实现的指针”或“pImpl”是一种 C++ 编程技巧,它将类的实现细节从对象表示中移除,放到一个分离的类中,并以一个不透明的指针进行访问。
|
||||
|
||||
使用pImpl惯用法的原因如下:
|
||||
|
||||
考虑如下例子:
|
||||
|
||||
```cpp
|
||||
class X
|
||||
{
|
||||
private:
|
||||
C c;
|
||||
D d;
|
||||
} ;
|
||||
```
|
||||
|
||||
变成pImpl就是下面这样子
|
||||
|
||||
```cpp
|
||||
class X
|
||||
{
|
||||
private:
|
||||
struct XImpl;
|
||||
XImpl* pImpl;
|
||||
};
|
||||
```
|
||||
|
||||
CPP定义:
|
||||
|
||||
```cpp
|
||||
struct X::XImpl
|
||||
{
|
||||
C c;
|
||||
D d;
|
||||
};
|
||||
```
|
||||
|
||||
- 二进制兼容性
|
||||
|
||||
开发库时,可以在不破坏与客户端的二进制兼容性的情况下向XImpl添加/修改字段(这将导致崩溃!)。 由于在向Ximpl类添加新字段时X类的二进制布局不会更改,因此可以安全地在次要版本更新中向库添加新功能。
|
||||
|
||||
当然,您也可以在不破坏二进制兼容性的情况下向X / XImpl添加新的公共/私有非虚拟方法,但这与标准的标头/实现技术相当。
|
||||
|
||||
- 数据隐藏
|
||||
|
||||
如果您正在开发一个库,尤其是专有库,则可能不希望公开用于实现库公共接口的其他库/实现技术。 要么是由于知识产权问题,要么是因为您认为用户可能会被诱使对实现进行危险的假设,或者只是通过使用可怕的转换技巧来破坏封装。 PIMPL解决/缓解了这一难题。
|
||||
|
||||
- 编译时间
|
||||
|
||||
编译时间减少了,因为当您向XImpl类添加/删除字段和/或方法时(仅映射到标准技术中添加私有字段/方法的情况),仅需要重建X的源(实现)文件。 实际上,这是一种常见的操作。
|
||||
|
||||
使用标准的标头/实现技术(没有PIMPL),当您向X添加新字段时,曾经重新分配X(在堆栈或堆上)的每个客户端都需要重新编译,因为它必须调整分配的大小 。 好吧,每个从未分配X的客户端也都需要重新编译,但这只是开销(客户端上的结果代码是相同的)。
|
||||
|
||||
|
||||
|
||||
> https://stackoverflow.com/questions/8972588/is-the-pimpl-idiom-really-used-in-practices
|
24
codingStyleIdioms/5_pImpl/a.cpp~
Normal file
24
codingStyleIdioms/5_pImpl/a.cpp~
Normal file
@@ -0,0 +1,24 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class C {
|
||||
|
||||
public:
|
||||
virtual void print();
|
||||
};
|
||||
|
||||
|
||||
class CC:public C {
|
||||
public:
|
||||
void print() {
|
||||
cout<<"CC"<<endl;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
|
||||
CC* c = dynamic_cast<CC *>(new C);
|
||||
//c->print();
|
||||
}
|
||||
|
||||
|
23
codingStyleIdioms/5_pImpl/noPimpl.cpp
Normal file
23
codingStyleIdioms/5_pImpl/noPimpl.cpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
class C {
|
||||
vector<int> v;
|
||||
string s;
|
||||
};
|
||||
class D {
|
||||
string s;
|
||||
};
|
||||
|
||||
class X {
|
||||
private:
|
||||
C c;
|
||||
D d;
|
||||
};
|
||||
|
||||
|
||||
int main() {
|
||||
|
||||
X x;
|
||||
}
|
48
codingStyleIdioms/5_pImpl/pimpl.cpp
Normal file
48
codingStyleIdioms/5_pImpl/pimpl.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
//
|
||||
// Created by light on 19-12-9.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
// pImpl: Pointer-to-Implementation
|
||||
|
||||
class private_foo;
|
||||
class foo {
|
||||
public:
|
||||
foo();
|
||||
|
||||
~foo();
|
||||
|
||||
void bar();
|
||||
|
||||
private:
|
||||
private_foo *pImpl;
|
||||
};
|
||||
|
||||
class private_foo {
|
||||
public:
|
||||
void bar() {
|
||||
cout<<"private_foo invoke bar funciton."<<endl;
|
||||
}
|
||||
|
||||
private:
|
||||
int m1;
|
||||
string m2;
|
||||
};
|
||||
|
||||
foo::foo() : pImpl(new private_foo()) {
|
||||
}
|
||||
|
||||
foo::~foo() {
|
||||
}
|
||||
|
||||
void foo::bar() {
|
||||
pImpl->bar();
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
foo f;
|
||||
f.bar();
|
||||
}
|
26
codingStyleIdioms/5_pImpl/pimplTime.cpp
Normal file
26
codingStyleIdioms/5_pImpl/pimplTime.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
class C {
|
||||
vector<int> v;
|
||||
string s;
|
||||
};
|
||||
class D {
|
||||
string s;
|
||||
};
|
||||
|
||||
class X {
|
||||
private:
|
||||
struct XImpl;
|
||||
XImpl* pImpl;
|
||||
};
|
||||
|
||||
struct X::XImpl {
|
||||
C c;
|
||||
D d;
|
||||
};
|
||||
|
||||
int main() {
|
||||
X x;
|
||||
}
|
11
codingStyleIdioms/5_pImpl/pimplTime.cpp~
Normal file
11
codingStyleIdioms/5_pImpl/pimplTime.cpp~
Normal file
@@ -0,0 +1,11 @@
|
||||
#include "pimplTime.h"
|
||||
|
||||
struct X::XImpl {
|
||||
C c;
|
||||
D d;
|
||||
}
|
||||
|
||||
int main() {
|
||||
X x;
|
||||
|
||||
}
|
19
codingStyleIdioms/5_pImpl/pimplTime.h
Normal file
19
codingStyleIdioms/5_pImpl/pimplTime.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
class C {
|
||||
vector<int> v;
|
||||
string s;
|
||||
};
|
||||
class D {
|
||||
string s;
|
||||
};
|
||||
|
||||
class X {
|
||||
private:
|
||||
struct XImpl;
|
||||
XImpl* pImpl;
|
||||
};
|
||||
|
||||
|
1
codingStyleIdioms/5_pImpl/timePimpl.cpp~
Normal file
1
codingStyleIdioms/5_pImpl/timePimpl.cpp~
Normal file
@@ -0,0 +1 @@
|
||||
#include <>
|
Reference in New Issue
Block a user