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 824581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:07:12+00:00 2026-05-15T03:07:12+00:00

I am trying to improve my knowledge on program architecture and recently arised a

  • 0

I am trying to improve my knowledge on program architecture and recently arised a question to me which is related with this pointers issues I posted recently.

The thing is that in a simple hierarchy in which you have Class A with a pointers to Class B and the last to Class C. Do not confuse the with the inheritage property of the Object Oriented programming but basically what I am saying is that Class C is the child of Class B and Class B is the child of Class A.

The point is that I want to be able to access directly from Class A to Class C (the grandson in the analogy) with pointers. Some other members pointed out this is poor design, basically because if a delete an instance of class C from class B collection would leave a pointer to “nothing” in the Class A collection. Then, how is this modelled properly?

Thank you a lot!

Julen.

  • 1 1 Answer
  • 1 View
  • 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-05-15T03:07:12+00:00Added an answer on May 15, 2026 at 3:07 am

    Instead of making class A aware of every class C, consider using the Composite Pattern:

    #include <boost/ptr_container/ptr_vector.hpp>
    #include <boost/foreach.hpp>
    #include <iostream>
    #include <stdexcept>
    
    //------------------------------------------------------------------------------
    class Component
    {
    public:
        typedef boost::ptr_vector<Component> Children;
    
        virtual void print() = 0;
        // Other operations
    
        virtual bool isLeaf() {return true;}
    
        virtual Children& children()
            {throw std::logic_error("Leaves can't have children");}
    };
    
    //------------------------------------------------------------------------------
    class Composite : public Component
    {
    public:
        void print()
        {
            BOOST_FOREACH(Component& child, children_)
            {
                child.print();
            }
        }
    
        bool isLeaf() {return false;}
    
        Children& children() {return children_;}
    
    private:
        Children children_;
    };
    
    //------------------------------------------------------------------------------
    class Nut : public Component
    {
    public:
        Nut(std::string info) : info_(info) {}
        void print() {std::cout << info_ << std::endl;}
    
    private:
        std::string info_;
    };
    
    //------------------------------------------------------------------------------
    class Bolt : public Component
    {
    public:
        Bolt(std::string info) : info_(info) {}
        void print() {std::cout << info_ << std::endl;}
    
    private:
        std::string info_;
    };
    
    //------------------------------------------------------------------------------
    class Wheel : public Composite
    {
    public:
        Wheel(std::string info) : info_(info) {}
        void print()
        {
            std::cout << info_ << std::endl;
            Composite::print();
        }
    
    private:
        std::string info_;
    };
    
    //------------------------------------------------------------------------------
    class Vehicle : public Composite
    {
    public:
        Vehicle(std::string info) : info_(info) {}
        void print()
        {
            std::cout << info_ << std::endl;
            Composite::print();
            std::cout << "\n\n";
        }
    
    private:
        std::string info_;
    };
    
    //------------------------------------------------------------------------------
    int main()
    {
        Wheel* wheel1 = new Wheel("Wheel1");
        wheel1->children().push_back(new Nut("Nut11"));
        wheel1->children().push_back(new Nut("Nut12"));
        wheel1->children().push_back(new Bolt("Bolt11"));
        wheel1->children().push_back(new Bolt("Bolt12"));
    
        Wheel* wheel2 = new Wheel("Wheel2");
        wheel2->children().push_back(new Nut("Nut21"));
        wheel2->children().push_back(new Nut("Nut22"));
        wheel2->children().push_back(new Bolt("Bolt21"));
        wheel2->children().push_back(new Bolt("Bolt22"));
    
        Vehicle bike("Bike");
        bike.children().push_back(wheel1);
        bike.children().push_back(wheel2);
    
        bike.print();
    }
    

    This program outputs:

    Bike
    Wheel1
    Nut11
    Nut12
    Bolt11
    Bolt12
    Wheel2
    Nut21
    Nut22
    Bolt21
    Bolt22
    

    Note that when bike.print() is called, print is called recursively on all children. This is how you can perform operations on all children without the grand-parent knowing about all its children.

    The Visitor Pattern works very well with the Composite pattern, so I suggest you read up on that one too. Especially if you have many operations that can be implemented in terms of more basic ones.

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

Sidebar

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.