- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
请参阅我的 PIMPL 继承实现。在派生类中,DerivedImpl继承自BaseImpl。
问题:指向 Impl 的指针是否应该像下面的代码一样只在基类中定义?如果是这样,每次我需要使用基指针时,我都必须将它转换为派生类型。然而,根据分析结果,静态转换 shared_ptr 看起来很昂贵,因为这种转换被广泛使用。并且 cast 函数不能在 header 中内联,因为它在那里不完整。
也许我犯了一些错误。或者使用智能指针有更好的实现吗?
// 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);
}
最佳答案
我假设您想要的正是您所描述的,取模实现细节:
公共(public)类的继承层次结构。
基于相应的实现类继承层次。
实现对全局命名空间和/或宏的可能使用,应限制在单独编译的单元中。
这是一个问题,派生类特定的初始化,会弹出,例如在 C++ 类中包装一组低级 GUI 小部件时。在许多其他情况下也是如此。有多种可能的解决方案,但您目前的解决方案是通过基类构造函数向上传递一个指向实现的指针,传递到最顶层的基类,在那里它被提供给派生类。
仍然,您不确定这是个好主意:
” Should the pointer to Impl only defined in base class like the following code?
是的,理想情况下应该如此,因为这种方法可确保始终完整构建可用的基类实例。这就是 C++ 构造函数的基本思想。在初始化(例如基类子对象)之后,您手头有一个工作对象,或者什么都没有,即异常或终止。
但是,这种方法会解决两个问题:
如何高效地提供派生类实现指针?
如何从基类实现派生实现?
后一个问题很容易通过为实现使用单独的头文件来解决。请记住,信息隐藏的目的不是让这些类的源代码在物理上不可访问,尽管这仍然是可能的。但要避免污染全局命名空间和宏域。
第一个问题,就是你实际问的问题,
” static cast a shared_ptr looks expensive according to profiling result because this cast is extensively used
这不是真正的问题。
向下转换函数只需要在代码的实现部分是可访问的,并且它们的源代码是可用的并且可以内联调用。
最后,只是建议,您应该使用unique_ptr
,或者没有智能指针,或者可能是自动克隆智能指针,而不是shared_ptr
,作为实现指针。因为您通常不希望公共(public)类实例的拷贝与原始实例共享其实现。除了实现没有状态的情况外,在这种情况下动态分配它意义不大。
例子:
基础.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
基础.Impl.hpp:
#pragma once
#include "Base.hpp"
class my::Base::Impl
{
public:
auto virtual foo() const -> char const* { return "Base"; }
virtual ~Impl() {}
};
基础.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"; }
};
派生.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
,并且应该在使用它们的每个翻译单元中进行相同的定义。与其将其划分为更多的 header ,更多的派生类应该/可以直接从 Base
实现向下转换,就像 Derived
所做的那样。
关于c++ - 使用智能指针继承的 pimpl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35191894/
是否可以有DropShadowEffect渲染阴影时忽略某些颜色?有某种蒙版(颜色选择性)阴影? 我的问题是什么阴影可以分配给整个视觉元素(图形)。它看起来像这样: 而且我要 注意没有阴影的网格线(除
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, over
有时,当我尝试使用 maven mvn clean install jetty:run 运行我的项目时,它会返回一个错误消息:“地址已在使用中”。 我正在使用 Intellij IDEA 14。 你能
我见过几个数据库缓存引擎,它们都非常愚蠢(即:将此查询缓存 X 分钟)并要求您在 之后手动删除整个缓存存储库>INSERT/UPDATE/DELETE 查询已执行。 大约 2 或 3 年前,我为我正在
我正在寻找例程或寻找错误容忍字符串比较的方法。 比方说,我们有测试字符串 Čakánka - 是的,它包含 CE 字符。 现在,我想接受以下任何字符串作为 OK: 恰坎卡 cákanká ÇaKaNK
大家好 我在 windows xp 系统上使用智能 gwt 2.3 专业版和 Mozilla。 我在上面做了测试应用程序,它运行成功。 现在我想为此应用程序使用开发人员控制台。 我的gwt.xml 编
说,我有 member this.Test (x: 'a) = printfn "generic" 1 member this.Test (x:
我正在努力寻找有关如何将应用程序部署到索尼智能 watch 的任何文档或教程(我正在尝试部署由索尼编写的开源“Eight Puzzle”应用程序,我已将其导入到 Eclipse 中。我有一个三星 S3
我正在寻找一个类似于标准格式化程序的智能 Java 字符串格式化程序: StringBuffer buffer = new StringBuffer(); Formatter form
我有一个智能 JScrollPane,但与其他所有解决方案不同,我想知道如果滚动 Pane 不在底部并且文本是否可以跟随(向上滚动)查看的组件(JTextArea)缓冲区已满。包含功能齐全的代码片段。
我有一个包含 18 个类(class)的项目。它们正在工作线程保存(已检查),没有递归调用,也没有任何复杂的计算。 但是我的IDEA总是说几个小时后 There is not enough memor
我想使用 Smart XLS 库在 C# 中处理 excel 文件(特别是因为它可以生成图表,而且它的价格明显低于 Aspose Cells),但我找不到任何好的例子。我只想知道如何创建和修改图表,有
我正在寻找一个完整的 java 日期管理库,它可以让我理解像这样的字符串: 明天中午 => 2011-10-20 12:00 今天下午 4 点 => 2011-10-20 16:00 等... 但如果
我想打开手机中的文件,通过 watch 中的 Action ... 问题是: Intent intent2 = new Intent(); intent2.setAction(android.cont
我想编译 Sony 为他的 SmartWatch 提供的示例。我按照此 page 中的说明进行操作.但是当我添加 SmartExtensionUtils 项目时,我看到了很多错误,例如:com.son
我有一个 SQL 表:名称、位置、体积 名称为字符串类型 位置是两个 float 类型的字段(纬度和经度) int 类型的体积 我想运行一个 SQL 查询,该查询将对特定范围内的所有位置进行分组并对所
我需要找到当我点击某物时调用的方法。项目很大,自己找会花不少时间。有什么办法可以做到吗?我正在使用 Android Studio,是的,这是我有史以来第一次使用其他人的项目。 最佳答案 我是怎么做到的
我在我的 iMac 上安装了智能 cvs,我已经从 cvs 更新了应用程序。然后我对我的代码和核心数据做了一些更改(核心数据更改是:从核心数据实体中删除一些属性),然后我尝试将更改提交给 cvs,但我
在我的 Java 项目中,我有一个类 Person,它存在于两个具有相同包名 (com.example.beans) 的不同 jar 中。 问题是这个类在jar1中是这样定义的: class Pers
已关闭。此问题旨在寻求有关书籍、工具、软件库等的建议。不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以
我是一名优秀的程序员,十分优秀!