Best structure for this container class?

B

BCC

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
 
K

Karl Heinz Buchegger

BCC said:
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;
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top