请参阅我继承的PIMPL实现。在派生类中,DerivedImpl继承自BaseImpl。
问题: 指向Impl的指针是否只在基类中定义,如下面的代码?如果是这样,每次我需要使用基指针时,我必须将它转换为派生类型。但是,根据分析结果静态转换shared_ptr看起来很昂贵,因为这种强制转换被广泛使用。并且强制转换函数不能在标题中内联,因为它在那里是不完整的。
也许我犯了一些错误。或者使用智能指针有更好的实现吗?
// Base.h
class BaseImpl; // pre-declaration
class Base
{
public:
Base();
explicit Base(BaseImpl* ptr);
~Base();
protected:
std::shared_ptr<BaseImpl> d_Ptr;
};
// baseimpl.h
class BaseImpl
{
double mDate;
};
// Derived.h
#include "Base.h"
class DerivedImpl;
class Derived :
public Base
{
public:
Derived();
~Derived();
std::shared_ptr<DerivedImpl> d_func();
const std::shared_ptr<DerivedImpl> d_func() const;
};
// Derived.cpp
#include "Derived.h"
#include "DerivedImpl.h"
Derived::Derived() : Base(new DerivedImpl())
{
}
Derived::~Derived()
{
}
std::shared_ptr<DerivedImpl> Derived::d_func()
{
return std::static_pointer_cast<DerivedImpl>(d_Ptr);
}
const std::shared_ptr<DerivedImpl> Derived::d_func() const
{
return std::static_pointer_cast<DerivedImpl>(d_Ptr);
}
答案 0 :(得分:5)
我假设您完全想要您描述的内容,模块化实现细节:
公共类的继承层次结构。
基于实现类的相应继承层次结构。
实现可能使用的全局命名空间和/或宏应该局限于单独编译的单元。
这是一个问题,派生类特定初始化,会弹出,例如在C ++类中包装一组低级GUI小部件时。还有许多其他情况。有很多可能的解决方案,但是你的解决方案是将指向实现的指针通过基类构造函数传递到最顶层的基础,并将其提供给派生类。
但是,你还不确定这是个好主意:
“指向Impl的指针是否只在基类中定义,如下面的代码?
是的,理想情况下应该这样,因为这种方法可确保始终完全构造可用的基类实例。这就是C ++构造函数的基本思想。在初始化之后(例如基类子对象),您要么手头有工作对象,要么没有,即异常或终止。
但是,这种方法可以解决两个问题:
如何有效地提供派生类实现指针?
如何从基类实现派生实现?
通过为实现提供单独的头文件,可以轻松解决后一个问题。请记住,隐藏信息的关键不在于使这些课程成为可能。源代码在物理上无法访问,尽管这仍然是可能的。但要避免污染全球命名空间和宏观土地。
第一个问题,就是你实际要问的问题,
“静态演员使用shared_ptr看起来很昂贵,因为这个演员阵容被广泛使用
不是一个真正的问题。
只需在代码的实现部分中访问向下转换函数,并且它们的源代码可用,并且可以内联调用。
最后,只是建议,你应该使用unique_ptr
,或者没有智能指针,或者可能是自动克隆智能指针,而不是shared_ptr
,用于实现指针。因为您通常不想要公共类实例的副本,所以要与原始实例共享其实现。除了实现没有状态的情况之外,在这种情况下动态分配它没有多大意义。
示例:
Base.hpp:的#pragma once
#include <memory>
namespace my {
using std::unique_ptr;
class Base
{
protected:
class Impl;
private:
unique_ptr<Impl> p_impl_;
protected:
auto p_impl() -> Impl* { return p_impl_.get(); }
auto p_impl() const -> Impl const* { return p_impl_.get(); }
Base( unique_ptr<Impl> p_derived_impl );
public:
auto foo() const -> char const*;
~Base();
Base();
};
} // namespace my
Base.Impl.hpp:的
#pragma once
#include "Base.hpp"
class my::Base::Impl
{
public:
auto virtual foo() const -> char const* { return "Base"; }
virtual ~Impl() {}
};
Base.cpp:的
#include "Base.Impl.hpp"
#include <utility> // std::move
using std::move;
using std::unique_ptr;
auto my::Base::foo() const
-> char const*
{ return p_impl()->foo(); }
my::Base::~Base() {}
my::Base::Base()
: p_impl_( new Impl() )
{}
my::Base::Base( unique_ptr<Impl> p_derived_impl )
: p_impl_( move( p_derived_impl ) )
{}
Derived.hpp:的
#pragma once
#include "Base.hpp"
namespace my {
class Derived
: public Base
{
protected:
class Impl;
Derived( unique_ptr<Impl> p_morederived_impl );
private:
auto p_impl() -> Impl*;
auto p_impl() const -> Impl const*;
public:
~Derived();
Derived();
};
} // namespace my
Derived.Impl.hpp:的
#pragma once
#include "Base.Impl.hpp"
#include "Derived.hpp"
class my::Derived::Impl
: public my::Base::Impl
{
public:
auto foo() const -> char const* override { return "Derived"; }
};
Derived.cpp:的
#include "Derived.Impl.hpp"
#include <utility> // std::move
using std::move;
using std::unique_ptr;
inline auto my::Derived::p_impl() -> Impl*
{ return static_cast<Impl*>( Base::p_impl() ); }
inline auto my::Derived::p_impl() const -> Impl const*
{ return static_cast<Impl const*>( Base::p_impl() ); }
my::Derived::~Derived() {}
my::Derived::Derived()
: Base( unique_ptr<Impl>( new Impl() ) )
{}
my::Derived::Derived( unique_ptr<Impl> p_morederived_impl )
: Base( move( p_morederived_impl ) )
{}
main.cpp中:的
#include "Derived.hpp"
#include <iostream>
using namespace std;
auto main() -> int
{
wcout << my::Derived().foo() << endl;
}
技术性:在类Derived
中,降级程序函数为private
,以防止它们被更多派生类直接使用。这是因为这些实现是inline
,并且应该在它们被使用的每个翻译单元中以相同的方式定义。而不是将其划分为更多的标题,更多的派生类应该/可以从Base
实现直接投射,就像Derived
一样。
答案 1 :(得分:1)
我认为你通过向BaseImpl
的用户展示Base
的详细信息,而不仅仅是从Base
派生的类,而是向所有人展示Base
一般来说DerivedImpl
的用户。出于完全相同的原因,// Base.h
class Base
{
public:
Base();
virtual ~Base();
// Add copy constructor and copy assignment operator too.
// Follow the rule of Three/rule of Five.
// Class that holds the implementation details of Base.
class Impl;
private:
// Never expose the details of Impl
// and never expose d_Ptr to clients.
Impl* d_Ptr;
};
也需要隐藏。
我建议如下:
// Base.cpp
class Base::Impl
{
// Add the necessary member variables and functions to facilitate
// Base's implementation
};
Base() : d_Ptr(new Impl)
{
}
~Base()
{
delete d_Ptr;
}
// Derived.h
#include "Base.h"
class Derived : public Base
{
public:
Derived();
~Derived();
// Add copy constructor and copy assignment operator too.
// Follow the rule of Three/rule of Five.
// Class that holds the implementation details of Derived.
// Has no relation to Base::Impl
class Impl;
private:
// Never expose the details of Impl
// and never expose d_Ptr to clients.
Impl* d_Ptr;
};
// Derived.cpp
class Derived::Impl
{
// Add the necessary member variables and functions to facilitate
// Derived's implementation
};
Derived() : d_Ptr(new Impl)
{
}
~Derived()
{
delete d_Ptr;
}
electedPersons = entities.PERSONS_DATA.Find(pid);
答案 2 :(得分:0)
过时的讨论,但是.. 我一直在思考在类的层次结构中使用 pimpl 惯用语的想法,但我不明白它如何在不向 impls 添加多态行为的情况下变得实用。鉴于 pimpl 成语的动机之一是避免 vtable,似乎一旦将多态性添加到 impls,您就不再使用经典意义上的 pimpl 成语。相反,它现在更像是一种桥接模式。它可以解决类似的问题,但其动机略有不同。