//
//pimpl 這種做法還保障了二進(jìn)制兼容性,讓動態(tài)庫的升級變得更容易。
//隱藏了實(shí)現(xiàn)的細(xì)節(jié).
//C++代碼
// A.h
class AImpl;
class A {
public:
A();
~A();
void func();
private:
A(const A&);
void operator=(const A&);
AImpl* impl_;
};
// A.cpp
class AImpl {
public:
void func();
private:
int
myFunc(int myParam);
private:
int _myVariable;
};
A::A()
: impl_(new AImpl)
{
}
A::~A()
{
delete impl_;
}
void A::func()
{
impl_->func();
}
// A.h
//pimplidiom 指向?qū)崿F(xiàn)的指針
//pimpl 這種做法還保障了二進(jìn)制兼容性,讓動態(tài)庫的升級變得更容易。
//隱藏了實(shí)現(xiàn)的細(xì)節(jié).
#pragma once
class A {
public:
A();
~A();
void func();
private:
A(const A&);
void operator=(const A&);
class AImpl; // 聲明,不是定義
AImpl* impl_;
};
// A.cpp
// 在這里定義,完全不暴露
#include "a.h"
class A::AImpl {
public:
void func(){
}
private:
int myFunc(int myParam);
private:
int _myVariable;
};
A::A()
: impl_(new AImpl)
{
}
A::~A()
{
delete impl_;
}
void A::func()
{
impl_->func();
}