Possibility to mix composite pattern and curiously recurring template pattern

前端 未结 3 1499
既然无缘
既然无缘 2021-01-17 14:00

I have a composite pattern implementation, used for GUI components:

class CObject {
private:

  CObject * m_pParent;  
  CObjectContainer * m_pChildren;

  v         


        
3条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-17 14:54

    From the snippet alone it is unclear why you need detach() to return a pointer to a delivered type.

    To take advantage of detach() returning a delivered type, it needs to be called using a reference to the delivered type anyway. Like this:

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

    But this can be replaced with equivalent that works even if detach is void:

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

    If you have a reference to the base type, you can't take advantage of detach() return type:

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

    For this reason it is unclear what are the advantages of the approach you are trying to implement.

    A side not is that the duplicate() method is smelly. It breaks when delivered class does not overwrite it, but uses the default implementation from the parent class. It can be a sign that something is wrong with the high level design.

提交回复
热议问题