简体   繁体   English

QExplicitlySharedPointer和继承

[英]QExplicitlySharedPointer and inheritance

What is the best way to use QExplicitlySharedPointer and inherited classes. 使用QExplicitlySharedPointer和继承的类的最佳方法是什么。 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> . QExplicitlySharedPointer<BaseClassPrivate>独自退出时,让我的d指针成为QExplicitlySharedPointer<BaseClassPrivate>而当我在此基类之上拥有Derived类时,我希望成为QExplicitlySharedPointer<DerivedClassPrivate>

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. 我尝试使DerivedClassPrivate从BaseClassPrivate继承,然后使d指针受到保护并在派生类中重新定义d指针,但是现在看来我有两个d指针副本,它们都位于定义它们的类的本地在...这不是我想要的。

What about this: 那这个呢:

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(); }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM