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

  • SEARCH
  • Home
  • 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 8557369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:33:33+00:00 2026-06-11T15:33:33+00:00

Suppose we have the simplest structure, classes Component , Leaf : Component and Composite

  • 0

Suppose we have the simplest structure, classes Component, Leaf : Component and Composite : Component. Each object of Leaf class has an int id which gives it its identity. The composite class would have sth like:

class Composite : public Component
{
public:
    void removeComponent(Component*);
// other stuff
private:
    std::vector<Component*> coll;
};

And leaf class sth like:

class Leaf : public Component
{
public:
    //stuff
    int getID();
private:
    int id;
};

And the question is how to define the function removeComponent(Component* cmp). cmp is actually a Leaf, but I need to access the Component vector coll, so it needs to be a Component (I think). The removeComponent method takes a Leaf object, and removes all other Leaves with the same ID, from the whole structure.

Now, I have thought of two ways (neither of which works :P):

First

void Composide::removeComponent(Component* cmp)
{
    std::vector<Component*>::const_iterator it;
    for(it = coll.begin(); it != coll.end(); ++it)
    {
        (*it)->removeComponent(cmp);
    // where removeComponent is defined as virtual in Component, but
    // the problem is that Leaf cannot erase itself from the collection
    }

}

Second

void Composide::removeComponent(Component* cmp)
    {
        std::vector<Component*>::const_iterator it;
        for(it = coll.begin(); it != coll.end(); ++it)
        {
            if((*it)->equals(*cmp))
            it = erase(it);
        // But here I can't define equals function for Composite elements,
    // so then I'd have to make functions which return the type of Component,
    // and in case of Composite call recursively the function and
    // in the case of Leaf call the equals() function and erase if needed.
    // This however looks like really bad OOP practice, and I'd like to avoid
    // using those methods which return type..
        }

    }

There has to be a neat, nice way to do this. I kind of think that the method should look something like the upper-mentioned 1st way, only I really don’t know how to enable the Leaf to delete itself from the vector. Help me please? 🙂

  • 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-11T15:33:34+00:00Added an answer on June 11, 2026 at 3:33 pm

    You seem to be getting confused as to what Component is exactly. Is it some business object, or is it a class representing a tree node? If it’s a tree node, then all tree nodes should support the same operations to allow easy recursion.

    As such I would move the definition of removeComponent() to the base Component class and make it virtual. You can provide an empty implementation in Component.

    Your composite implementation is then simply:

    void Composide::removeComponent(Component* cmp)
    {
        std::vector<Component*>::const_iterator it;
        for(it = coll.begin(); it != coll.end(); ++it)
        {
            if((*it)->equals(*cmp))
               it = erase(it);
            else
               (*it)->removeComponent(cmp);
        }
    }
    

    EDIT: Regarding Id’s

    Again, I think you might be muddling two concepts, Component id’s, and Components.
    (Perhaps it would be better if you renamed Component to TreeItem?)

    Your current removeComponent() function takes a Component pointer, thus inferring that any Component can be removed from the tree (including Composites). This seems correct to me. You might well need to remove Composites. Thus, you can simply compare pointers.

    However, you then seem to be comparing Id’s (via an assumed equality overload) that only Leafs have.

    If you want to provide an additional function to remove by Id, then I would move the GetID() function to the base Component class too, and compare against that. Composite objects can return -1 or some other null marker.

    eg.

    void Composite::getID()
    {
       return -1;
    }
    
    void Composide::removeComponent(Component* cmp)
    {
        std::vector<Component*>::const_iterator it;
        for(it = coll.begin(); it != coll.end(); ++it)
        {
            if((*it) == cmp)
               it = erase(it);
            else
               (*it)->removeComponent(cmp);
        }
    }
    
    void Composite::removeComponentById(int id)
    {
        std::vector<Component*>::const_iterator it;
        for(it = coll.begin(); it != coll.end(); ++it)
        {
            if((*it)->getID() == id)
               it = erase(it);
            else
               (*it)->removeComponentById(id);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a static method of my class that returns an object of
Suppose I have a class Baz that inherits from classes Foo and Bar ,
Suppose I have a custom object set up in a class similar to this.
Suppose I have a class: class Boy { int age = 25; } I
Suppose I have a simple model, such as Record: @Model public class Record {
Suppose I have this: public class Unit<MobileSuit, Pilot> { ... List<MobileSuit> mobileSuits; List<Pilot> pilots;
Suppose we have the Lyric model: class Lyric < ActiveRecord::Base belongs_to :song end and
Suppose I have a grid of squared defined like so in a class: Square
Suppose I just created a package example and have two classes inside it, Main
Suppose I have a string which contains more than 10,000 words, for example, it

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.