C++ 使用智能指针继承的pimpl
请参阅我用继承实现的PIMPL。在派生类中,DerivedImpl继承自BaseImpl 问题: 指向Impl的指针是否应该像下面的代码那样仅在基类中定义?如果是这样,每次我需要使用基指针时,我都必须将它转换为派生类型。然而,根据分析结果,静态强制转换共享的_ptr看起来很昂贵,因为这种强制转换被广泛使用。而且cast函数不能内联到header中,因为它在那里不完整 也许我犯了一些错误。或者使用智能指针是否有更好的实现C++ 使用智能指针继承的pimpl,c++,inheritance,pimpl-idiom,C++,Inheritance,Pimpl Idiom,请参阅我用继承实现的PIMPL。在派生类中,DerivedImpl继承自BaseImpl 问题: 指向Impl的指针是否应该像下面的代码那样仅在基类中定义?如果是这样,每次我需要使用基指针时,我都必须将它转换为派生类型。然而,根据分析结果,静态强制转换共享的_ptr看起来很昂贵,因为这种强制转换被广泛使用。而且cast函数不能内联到header中,因为它在那里不完整 也许我犯了一些错误。或者使用智能指针是否有更好的实现 // Base.h class BaseImpl; // pre-decla
// Base.h
class BaseImpl; // pre-declaration
class Base
{
public:
Base();
explicit Base(BaseImpl* ptr);
~Base();
protected:
std::shared_ptr<BaseImpl> d_Ptr;
};
//派生的.h
#包括“Base.h”
类派生impl;
派生类:
公共基地
{
公众:
派生();
~Derived();
std::shared_ptr d_func();
常量std::shared_ptr d_func()常量;
};
//Derived.cpp
#包括“派生的.h”
#包括“DerivedImpl.h”
派生::派生():基(新的DerivedImpl())
{
}
派生::~派生()
{
}
std::shared_ptr派生::d_func()
{
返回std::静态指针转换(d_Ptr);
}
常量std::shared_ptr派生::d_func()常量
{
返回std::静态指针转换(d_Ptr);
}
我认为,向Base
的用户公开BaseImpl
的详细信息,不仅是从Base
派生的类,而且是向Base
的所有用户公开BaseImpl
的详细信息,这违背了本书的目的。出于完全相同的原因,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;
};
我假设您想要的正是您所描述的模块实现细节:
- 公共类的继承层次结构
- 基于实现类的相应继承层次结构
- 实现对全局命名空间和/或宏的可能使用应限于单独编译的单元
这是一个问题,<强>派生类初始化初始化<强>,例如在C++类中封装一组低级别GUI小部件时弹出。在许多其他情况下也是如此。有很多可能的解决方案,但到目前为止,您的解决方案是通过基类构造函数向上传递一个指向实现的指针,指向最顶端的基类,在那里它被提供给派生类
不过,你不确定这是个好主意: “指向Impl的指针是否应仅在基类中定义,如以下代码所示 是的,理想情况下应该这样做,因为这种方法确保始终完全构造可用的基类实例。这就是C++构造函数的基本思想。初始化后(例如,基类子对象的初始化),要么手头有一个工作对象,要么什么都没有,即异常或终止 但是,这种方法有两个问题:- 如何有效地提供派生类实现指针
- 如何从基类实现派生实现
unique\u ptr
,或者不使用智能指针,或者自动克隆智能指针,而不是shared\u ptr
,作为实现指针。因为您通常不需要公共类实例的副本,所以需要将其实现与原始实例共享。除了实现没有状态的情况,在这种情况下,动态地分配它不是很有意义
例如: Base.hpp: Base.cpp: 派生的.cpp:
#包括“派生的.Impl.hpp”
#include//std::move
使用std::move;
使用std::unique\u ptr;
内联自动我的::派生::p_impl()->impl*
{return static_cast(Base::p_impl());}
内联自动我的::派生::p_impl()常量->impl常量*
{return static_cast(Base::p_impl());}
我的::派生::~Derived(){}
my::派生::派生()
:Base(唯一的\u ptr(新的Impl()))
{}
my::Devided::Devided(唯一的\u ptr p\u moreDevided\u impl)
:Base(移动(p_morederived_impl))
{}
main.cpp:
#包括“派生的.hpp”
#包括
使用名称空间std;
auto main()->int
{
wcout您真的需要从d_func
返回shared_ptr
吗?我想不出任何一种情况下,为一组类实例拥有一个共享的动态分配实现是有意义的。您应该只使用unique_ptr
@immibis,我对此不确定。如果d_func返回一个原始poitner,是否有任何r内存泄漏的isk?(例如d_func()->aMemberFunc()抛出)@YohWang Ifd_func()->aMemberFunc())
throws,那么这与任何内存泄漏无关。内存泄漏是指您的程序分配了某些内容而忘记取消分配它。@Cheersandhth.-Alf,不推荐。我删除了它们。@RSahu在每个派生类中都有指向Impl的指针似乎效率不高。内存将被重复分配以构造派生class(代码中有两个“new Impl”)和DerivedImpl可能会从BaseImpl继承一些成员。@YohWang,当你使用Pimpl习惯用法时,你会承担额外的分配和释放开销。你可以做你正在尝试的事情,但在我看来,它违背了Pimpl习惯用法背后的原则。@RSahu,有时IML会有一个错误
// 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);
}
// 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;
}
#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
#pragma once
#include "Base.hpp"
class my::Base::Impl
{
public:
auto virtual foo() const -> char const* { return "Base"; }
virtual ~Impl() {}
};
#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 ) )
{}
#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
#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"; }
};
#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 ) )
{}
#include "Derived.hpp"
#include <iostream>
using namespace std;
auto main() -> int
{
wcout << my::Derived().foo() << endl;
}