12

我有一个复合模式实现,用于 GUI 组件:

class CObject {
private:

  CObject * m_pParent;  
  CObjectContainer * m_pChildren;

  void private_foo() {
    this->foo();
    //Calls private_foo for each child in container.
    m_pChildren->foo();
  }

public:
  virtual void foo() {
    //empty for base class
  }

  virtual CObject * duplicate() {
    //Do duplication code
    return new CObject(*this);
  }

  virtual CObject * detach() {
    //Remove this object (along with it's children)
    //from current tree.
    m_pParent->RemoveChild(this);
    m_pParent = nullptr;
    return this;
  }
}

class CSpecificObject : public CObject {
public:
  virtual void foo() {
    //Specific code for this class
  }

  virtual CSpecificObject * duplicate() {
    //Overload, but the code only calls diferent constructor
    return new CSpecificObject(*this);
  }

  virtual CSpecificObject * detach() {
    //Note the code is identical.
    m_pParent->RemoveChild(this);
    m_pParent = nullptr;
    return this;
  }
}

不幸的是,继承类的数量迅速增加,重复的代码(在给定的例子中只有 detach() 方法)让我头疼。

有没有办法干净地实现 detach() 方法,保持返回类型与调用它的对象相同?

我在考虑 CRTP,但我想不出一种方法来保持动态多态性和编译时多态性:

template <Child>
class CObject {
private:
  ...
  Child * detach() {
    m_pParent->RemoveChild(this);
    m_pParent = nullptr;
    return static_cast<Child*>(this);
  }
  ...
}

//Array of CObject* pointers is no longer possible.
4

3 回答 3

7

您可以添加一层抽象:

class CObjectBase
{
    public:
        // Other methods...
        virtual CObjectBase* detach() = 0;
        virtual CObjectBase* duplicate() const = 0;
};

template <typename Child>
class CObject : public CObjectBase
{
    public:
        // ...
        Child* duplicate() const
        {
            return new Child(*static_cast<Child*>(this));
        }

        Child* detach()
        {
            m_pParent->RemoveChild(this);
            m_pParent = nullptr;
            return static_cast<Child*>(this); // Cast needed here (inherent to CRTP)
        }
        std::vector<CObjectBase*> children; // Array possible now
        // ...
};

class MyObject : public CObject<MyObject>
{
    // ...
};

在自然语言中:所有对象的接口(CObjectBase)都有其后代(CObject<Child>)的部分实现,它只需要继承这个部分实现,减少复制代码的数量。

于 2012-12-13T20:33:54.683 回答
1

我在考虑 CRTP,但我想不出一种方法来保持动态多态性和编译时多态性

您可以通过使用 CRTP 样式基类为某些接口提供默认虚拟实现来混合它们。

因此,您可以聚合 CRTP 基本实现(可能配置有额外的“策略”模板参数),并且仍然能够覆盖继承类中的特定行为。

Microsoft 的ATL 库经常使用它。我还在我的STTCL 状态机库中使用了这种技术。

于 2012-12-13T20:35:02.733 回答
1

仅从代码片段就不清楚为什么需要detach()返回指向传递类型的指针。

要利用detach()返回已交付类型的优势,无论如何都需要使用对已交付类型的引用来调用它。像这样:

CSpecificObject* specific_object = new SpecificObject();
// ...
specific_object->detach()->method_declared_in_specific_object();

但这可以替换为即使 detach 无效的等效项:

specific_object->detach();
specific_object->method_declared_in_specific_object();

如果您有对基类型的引用,则不能利用detach()返回类型:

CObject* specific_object = new SpecificObject();
//...
// !!! Won't compile:
specific_object->detach()->method_declared_in_specific_object(); 

出于这个原因,目前尚不清楚您尝试实施的方法的优点是什么。

一个不好的地方是duplicate()方法很臭。当交付的类不覆盖它时它会中断,而是使用父类的默认实现。这可能表明高层设计有问题。

于 2012-12-14T10:02:09.847 回答