Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8982851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:40:58+00:00 2026-06-15T20:40:58+00:00

I have a composite pattern implementation, used for GUI components: class CObject { private:

  • 0

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

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

Unfortunately the number of inherited classes increases rapidly and the duplicate code (in given example only the detach() method) is giving me a headache.

Is there a way to cleanly implement detach() methods, keeping the return type the same as the object, on which it is called?

I was thinking about CRTP, but I can not think of a way to keep the dynamic polymorphism along with compile time polymorphism:

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.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T20:41:00+00:00Added an answer on June 15, 2026 at 8:41 pm

    You can add one level of abstraction:

    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>
    {
        // ...
    };
    

    In natural language: an interface for all objects (CObjectBase) have a partial implementation for its descendants (CObject<Child>), which just have to inherit this partial implementation, decreasing the amount of replicated code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a simple composite pattern structure: abstract class User leaf class PersonalUser
I have a class that is a leaf in the composite pattern. This class
I have a Product class which has a 1-to-many association to itself (composite pattern).
I have implemented a simple Composite pattern using SplObjectStorage, like the example above: class
There are a lot of LINQ-based implementations of the Composite Specification Pattern. I have
Suppose I have a composite class PharmaProduct (which represents the product hierarchy of a
I'm using the composite pattern to have reusable elements to build a page. for
I am creating a class that implements the composite pattern; the class is supposed
I am using a composite pattern as shown in the class diagram below. Basically
I have composite project which contains: C++ library for Linux/Windows, C++/CLI library based on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.