Best structure for this container class?

Discussion in 'C++' started by BCC, Jul 23, 2003.

  1. BCC

    BCC Guest

    Hi,

    This is kind of a followup to a previous question...

    I have a container class that has a load of variables (~100).

    For example, I have groupings like this:
    class CContainer {
    m_circle_area;
    m_circle_position;
    m_circle_distance;

    m_square_area;
    m_square_position;
    m_square_distance;
    // etc, etc
    };

    Each triplet describes the properties of a particular type of object.

    Conceptually, what I need to do is if my object is of type 'circle' it only
    accesses the properties related to circle from the container class. Maybe
    an analogy would help... Imagine a room where you have a chest of drawers.
    Each drawer represents an object type, and contains all variables related to
    that type. So the 'circle' drawer will have circle area, position, and
    distance as well as maybe 'oval' area position and distance.

    As a new object comes into the room, it opens the drawer corresponding to
    its type and utilizes the variables it finds there. The object then leaves,
    and new object comes in and opens its drawer (whether it be the same type or
    not) and does its thing. This repeats.

    So, every room needs a chest with a full complement of properties for all
    objects. Each drawer needs to contain only the properties relating to a
    particular object, and properties are grouped according to the type of
    object they represent.

    At the moment, Im thinking about creating a class for each object just for
    properties, and maybe putting all related property sets in an array
    according to object type:
    circle->circleArray[0].m_type_associated;
    circle->circleArray[0].m_name;
    circle->circleArray[0].m_area;
    circle->circleArray[0].m_position;
    circle->circleArray[0].m_distance;
    etc.

    Basically, I need any object to come into any room, retrieve the applicable
    subset of variables and then loop/iterate through them to use.

    I dont know though. Any thoughts on a good structure to use? Best way to
    design this? Anything?

    Thanks,
    Bryan
     
    BCC, Jul 23, 2003
    #1
    1. Advertising

  2. BCC wrote:
    >
    > Hi,
    >
    > This is kind of a followup to a previous question...
    >
    > I have a container class that has a load of variables (~100).
    >
    > For example, I have groupings like this:
    > class CContainer {
    > m_circle_area;
    > m_circle_position;
    > m_circle_distance;
    >
    > m_square_area;
    > m_square_position;
    > m_square_distance;
    > // etc, etc
    > };
    >
    > Each triplet describes the properties of a particular type of object.
    >
    > Conceptually, what I need to do is if my object is of type 'circle' it only
    > accesses the properties related to circle from the container class. Maybe
    > an analogy would help... Imagine a room where you have a chest of drawers.
    > Each drawer represents an object type, and contains all variables related to
    > that type. So the 'circle' drawer will have circle area, position, and
    > distance as well as maybe 'oval' area position and distance.
    >
    > As a new object comes into the room, it opens the drawer corresponding to
    > its type and utilizes the variables it finds there. The object then leaves,
    > and new object comes in and opens its drawer (whether it be the same type or
    > not) and does its thing. This repeats.
    >
    > So, every room needs a chest with a full complement of properties for all
    > objects. Each drawer needs to contain only the properties relating to a
    > particular object, and properties are grouped according to the type of
    > object they represent.
    >
    > At the moment, Im thinking about creating a class for each object just for
    > properties, and maybe putting all related property sets in an array
    > according to object type:
    > circle->circleArray[0].m_type_associated;
    > circle->circleArray[0].m_name;
    > circle->circleArray[0].m_area;
    > circle->circleArray[0].m_position;
    > circle->circleArray[0].m_distance;
    > etc.
    >
    > Basically, I need any object to come into any room, retrieve the applicable
    > subset of variables and then loop/iterate through them to use.
    >
    > I dont know though. Any thoughts on a good structure to use? Best way to
    > design this? Anything?


    First of all, is this really what you want to do? An object comes into
    a room and opens a drawer ....
    .... or is it just a prosa text, where you think you need to do that. A
    little bit more context would help to decide that. When I started reading
    your description, I thought: another guy discovering polymorphism, but
    I'm not so sure anymore.

    Anyway:

    #pragma warning( disable: 4786 )

    #include <iostream>
    #include <string>
    #include <map>

    class Room;

    class Primitive
    {
    public:
    virtual void Enter( const Room& TheRoom ) = 0;
    };

    class Circle : public Primitive
    {
    public:
    virtual void Enter( const Room& TheRoom );
    };

    class Square : public Primitive
    {
    public:
    virtual void Enter( const Room& TheRoom );
    };

    class Chest
    {
    public:
    std::map< std::string, double > m_Properties;

    void AddProp( std::string Label, double Value )
    { m_Properties[Label] = Value; }

    bool FindProp( std::string Label, double& Value ) const
    { if( m_Properties.find( Label ) != m_Properties.end() ) {
    Value = m_Properties.find( Label )->second;
    return true;
    }
    return false;
    }
    };

    class Room
    {
    public:
    Room( std::string Name ) : m_Name( Name ) {}

    std::string Name() const { return m_Name; }

    void AddProp( std::string Label, double Value )
    { m_Chest.AddProp( Label, Value ); }

    bool FindProp( std::string Label, double& Value ) const
    { return m_Chest.FindProp( Label, Value ); }

    protected:
    std::string m_Name;
    Chest m_Chest;
    };

    void Circle::Enter( const Room& TheRoom )
    {
    std::cout << "Hi, I am a circle\n";
    std::cout << " In room " << TheRoom.Name() << " I found:\n";

    double Area;
    double Distance;

    if( TheRoom.FindProp( "Circ_Area", Area ) )
    std::cout << " Area: " << Area << "\n";
    else
    std::cout << " ** No Area **\n";

    if( TheRoom.FindProp( "Circ_Dist", Distance ) )
    std::cout << " Distance: " << Distance << "\n";
    else
    std::cout << " ** No Distance **\n";
    }

    void Square::Enter( const Room& TheRoom )
    {
    std::cout << "Hi, I am a square\n";
    std::cout << " In room " << TheRoom.Name() << " I found:\n";

    double Area;
    double Distance;

    if( TheRoom.FindProp( "Square_Area", Area ) )
    std::cout << " Area: " << Area << "\n";
    else
    std::cout << " ** No Area **\n";

    if( TheRoom.FindProp( "Square_Dist", Distance ) )
    std::cout << " Distance: " << Distance << "\n";
    else
    std::cout << " ** No Distance **\n";
    }


    int main()
    {
    Room Lobby( "Lobby" );

    Lobby.AddProp( "Circ_Area", 20.0 );
    Lobby.AddProp( "Circ_Dist", 80.0 );
    Lobby.AddProp( "Square_Area", 200.0 );
    Lobby.AddProp( "Square_Dist", 5.0 );

    Room Hallway( "Hallway" );

    Hallway.AddProp( "Circ_Area", 120.0 );
    Hallway.AddProp( "Circ_Dist", 180.0 );
    Hallway.AddProp( "Square_Area", 1200.0 );
    Hallway.AddProp( "Square_Dist", 15.0 );

    Circle TheCirc;
    Square TheSquare;

    TheCirc.Enter( Hallway );
    TheSquare.Enter( Lobby );
    TheSquare.Enter( Hallway );
    TheCirc.Enter( Lobby );

    return 0;
    }


    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Jul 24, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vivi Orunitia
    Replies:
    11
    Views:
    4,510
    Martijn Lievaart
    Feb 4, 2004
  2. Maitre Bart
    Replies:
    2
    Views:
    536
    Maitre Bart
    Feb 11, 2004
  3. Steven T. Hatton
    Replies:
    4
    Views:
    3,939
    Rob Williscroft
    Dec 5, 2004
  4. Replies:
    4
    Views:
    816
    Daniel T.
    Feb 16, 2006
  5. Replies:
    4
    Views:
    278
    Bruno Desthuilliers
    Feb 7, 2008
Loading...

Share This Page