473,799 Members | 3,422 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_positi on;
m_circle_distan ce;

m_square_area;
m_square_positi on;
m_square_distan ce;
// 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_associa ted;
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 2236


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_positi on;
m_circle_distan ce;

m_square_area;
m_square_positi on;
m_square_distan ce;
// 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_associa ted;
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.fi nd( Label ) != m_Properties.en d() ) {
Value = m_Properties.fi nd( 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.FindPro p( 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.FindPro p( "Circ_Area" , Area ) )
std::cout << " Area: " << Area << "\n";
else
std::cout << " ** No Area **\n";

if( TheRoom.FindPro p( "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.FindPro p( "Square_Are a", Area ) )
std::cout << " Area: " << Area << "\n";
else
std::cout << " ** No Area **\n";

if( TheRoom.FindPro p( "Square_Dis t", 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_Are a", 200.0 );
Lobby.AddProp( "Square_Dis t", 5.0 );

Room Hallway( "Hallway" );

Hallway.AddProp ( "Circ_Area" , 120.0 );
Hallway.AddProp ( "Circ_Dist" , 180.0 );
Hallway.AddProp ( "Square_Are a", 1200.0 );
Hallway.AddProp ( "Square_Dis t", 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
1442
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 class, which holds a company eomployee class , the Company class and the Data holding class. I am somehow trying to pass the container class into the preset frame as seen in the attached file (Jdeveloper package file) hope someone can give some...
5
4811
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 { int source : 3; int length : 4; int count : 9
41
3091
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 degenerate case, and can also be stored. Here's the web-page, with complete source, illustration, and example usage. Take a look-see, and post your critique! http://home.comcast.net/~anglewyrm/hat.html
6
1549
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 the list. Is it the best way to check existance of an item in a list ? Thanks,
2
1729
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
1814
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 webservice that returns a structure. On the client side I have added the webservice as a reference and created a class that contains the structure as a member as well as some other variables. The structure looks something like:
5
2071
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 to return the entry as a dereferenced pointer so it becomes a class or return a reference to the dereferenced class? I believe that it would be best to dereference the class pointer retrieved from the map and then send it back as a reference to...
11
1690
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, the behaviours of std::vector::iterator can not meet my requirements. I need to redefine the vector::iterator's behavious such as ++. It means "next position in the object sequence" in STL but now I redefine it to mean "next position within a 2-d...
3
2401
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 objects in a container class like a vector or list while still being able to access the individual subclass (virtual) functions? In my program, it is as if the functions aren't virtual. Longwinded version:
0
9688
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10259
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10030
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9077
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7570
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5589
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4145
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.