Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Best structure for this container class?

Reply
Thread Tools

Best structure for this container class?

 
 
BCC
Guest
Posts: n/a
 
      07-23-2003
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




 
Reply With Quote
 
 
 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      07-24-2003


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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
container inside container in stl wolverine C++ 2 07-24-2006 03:08 PM
Copy elements from one STL container to another STL container Marko.Cain.23@gmail.com C++ 4 02-16-2006 05:03 PM
std::transform container => std::abs(container) Steven T. Hatton C++ 4 12-05-2004 07:10 AM
STL: container's values setup by another container Maitre Bart C++ 2 02-11-2004 12:11 AM
std::container::iterator vs std::container::pointer Vivi Orunitia C++ 11 02-04-2004 08:09 AM



Advertisments