Solving design involving multiple inheritance and composite classes in c++

前端 未结 4 1788
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-09 05:14

I have struggled with this design problem for some time. I will do my best to explain what I am trying to do and the various approached that I have seen, what I am trying and wh

4条回答
  •  时光说笑
    2021-02-09 06:08

    I think you have to have one class Galaxy, one class SolarSystem, etc. And yours GalaxyOne, GalaxyTwo SolarSystemOne, SolarSystemTwo etc. are only different objects instantited from these classes.

    class SolarSystem { /* … */ };
    class Planet{ /* … */ };
    class Moon{ /* … */ };
    
    class Galaxy{ /* … */
     public: // Galaxy(...?...){for (...) {read data??; solarSystem.push_back(createSolarSystem(data)); }
     void AddSolarSystem(SolarSystem* ss){solarSystem.push_back(ss);}
     protected:
       std::vector solarSystems;
       /* … */
    };
    

    ....

    Galaxy GalaxyOne, GalaxyTwo;
    

    If we have no way to use this simple aproach... Lets see yours:

    class GalaxyOneTwo: public GalaxyOne, 
                        public GalaxyTwo, 
                        public PrimitiveGalaxy{
        /* … */
     using PrimitiveGalaxy::solarSystems;
     GalaxyOneTwo(){solarSystems.reserve(10);}
    };
    

    Here you have three private vectors: (using using you make it direct accesible)

    std::vector GalaxyOne::solarSystems;
    std::vector GalaxyTwo::solarSystems;
    std::vector solarSystems; //GalaxyOneTwo::solarSystems;
    

    Is this what you need? Make it protected? enter image description here

提交回复
热议问题