点击查看代码
//定义成宏 publicDefine.h
// PIMPL模式声明
#define PIMPL_DEFINE(Classname) \
const Classname##Impl* GetImpl() const; \
Classname##Impl* GetImpl(); \
std::unique_ptr<Classname##Impl> m_pData;
// PIMPL模式实现
#define PIMPL_IMPL(Classname) \
Classname##Impl* Classname::GetImpl() \
{ \
return m_pData.get(); \
} \
const Classname##Impl* Classname::GetImpl() const \
{ \
return m_pData.get(); \
}
//.h
class Entity final
{
public:
Entity(int nId, const std::string& entityName);
~Entity();
void SetName(const std::string& name);
std::string GetName() const;
void SetId(int id);
int GetId() const;
private:
PIMPL_DEFINE(Entity);
};
/////////////////////////////////
.cpp
class EntityImpl
{
public:
EntityImpl(int nId, const std::string& entityName) : m_nId(nId),
m_sEntityName(entityName),
bIsVisile(false)
{
}
int m_nId;
std::string m_sEntityName;
bool bIsVisile;
};
////////////////////////////////
PIMPL_IMPL(Entity);
Entity::Entity(int nId, const std::string& entityName) : m_pData(std::make_unique<EntityImpl>(nId, entityName))
{
}
GCK::Entity::~Entity()
{
}
void Entity::SetName(const std::string& name)
{
GetImpl()->m_sEntityName = name;
}
std::string Entity::GetName() const
{
return GetImpl()->m_sEntityName;
}
void Entity::SetId(int id)
{
GetImpl()->m_nId = id;
}
int Entity::GetId() const
{
return GetImpl()->m_nId;
}