473,386 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Best structure for this container class?

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


Jul 19 '05 #1
1 2210


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
kb******@gascad.at
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Shaun Marshall | last post by:
Hope someone can help, for an assignment i was given a starter application package, with that starter package i had to create some employee classes and test harnesses. below iv pasted my Container...
5
by: MPowell | last post by:
I'm going through the Koeing book Accelerated C++ in an attempt to understand Container classes. Of course I'm going through a paradigm shift from C to C++. So now I've got struct Header {...
41
by: AngleWyrm | last post by:
I have created a new container class, called the hat. It provides random selection of user objects, both with and without replacement, with non-uniform probabilities. Uniform probabilities are a...
6
by: A.M | last post by:
Hi, I have a list of strings (40-90 items) and i want to check if a specific string exists in the list or not. I can use a hash table, if the table returns null then the item doesn't exist in...
2
by: ahaupt | last post by:
Hi all, Just a quick one: At the moment I use an ArrayList to store 'n Tree classes' children. Is this the best/quickest structure to use? What do you guys use? Best, Andre
3
by: Ren | last post by:
Hi all, I'm still rather new to .NET so I hope you'll bear with me as I try and explain my question. I am writing an ASP.NET application using VB.NET. I am accessing a web method from a...
5
by: Tom | last post by:
If I have a container class that has a map member which stores pointers to objects that have been created via the new operator and I have a method that returns a entry in the map, would it be best...
11
by: food4uk | last post by:
Dear all : I am not good at programming, please give a hand. My data structure is very similar as an array. I actually can use the std::vector as container to organize my data objects. However,...
3
by: orkonoid | last post by:
Hello, I am having trouble with a Polymorphism issue using container classes. I have a longwinded and shortwinded version of my question: Shortwinded version: How can I store base class...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.