QExplicitlySharedPointer

QExplicitlySharedPointer

本文介绍了QExplicitlySharedPointer和继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 QExplicitlySharedPointer 和继承类的最佳方法是什么。我想当BaseClass退出它自己有一个我的d指针 QExplicitlySharedPointer< BaseClassPrivate> ,当我有一个Derived类在这个基类顶部喜欢有一个 QExplicitlySharedPointer< DerivedClassPrivate>

What is the best way to use QExplicitlySharedPointer and inherited classes. I would like when the BaseClass exits on it's own to have a my d pointer be QExplicitlySharedPointer<BaseClassPrivate> and when I have a Derived class on top of this base class I'd like to have d be a QExplicitlySharedPointer<DerivedClassPrivate>.

我试图让DerivedClassPrivate继承自BaseClassPrivate,然后使d指针保护并重新定义我的派生类中的d指针,但现在看来

I tried making DerivedClassPrivate inherit from BaseClassPrivate, and then make the d pointer protected and re-define the d-pointer in my derived class, but it seems now that I have two copies of the d-pointer both local to the class they are defined in... which is not what I want.

推荐答案

这是什么:

template< typename P = BaseClassPrivate >
class BaseClass
{
public:
  void myBaseFunc() { d->myBaseFunc(); }
protected:
  QExplicitlySharedDataPointer< P > d;
};

class DerivedClass : public BaseClass< DerivedClassPrivate >
{
public:
  void myDerivedFunc() { d->myDerivedFunc(); }
};

这篇关于QExplicitlySharedPointer和继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 22:56