QExplicitlySharedPointer and inheritance

老子叫甜甜 提交于 2019-12-24 21:40:20

问题


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>.

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.


回答1:


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


来源:https://stackoverflow.com/questions/2693319/qexplicitlysharedpointer-and-inheritance

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!