gpt4 book ai didi

c++ - 使用智能指针继承的 pimpl

转载 作者:搜寻专家 更新时间:2023-10-31 00:11:05 26 4
gpt4 key购买 nike

请参阅我的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com