how to handle groups of objects

B

Brian Dumas

This is a little long, sorry.
Let's say I have a class

class TMyClass
{
void funca();
void funcb();
};

and then I have objects

TMyClass* A = new TMyClass;
TMyClass* B = new TMyClass;
TMyClass* C = new TMyClass;

Sometimes, I need to call all three objects funca, like this :

A->funca();
B->funca();
C->funca();

So finally to the question - is there a way to combine these objects in a
"Meta" object that could do
something like this :
TMyClass* Meta=new TMyClass;
Meta->funca();

and have it run A, B, and C objects funca called?

THanks, Brian Dumas
 
H

Howard

Brian Dumas said:
This is a little long, sorry.
Let's say I have a class

class TMyClass
{
void funca();
void funcb();
};

and then I have objects

TMyClass* A = new TMyClass;
TMyClass* B = new TMyClass;
TMyClass* C = new TMyClass;

Sometimes, I need to call all three objects funca, like this :

A->funca();
B->funca();
C->funca();

So finally to the question - is there a way to combine these objects in a
"Meta" object that could do
something like this :
TMyClass* Meta=new TMyClass;
Meta->funca();

and have it run A, B, and C objects funca called?

THanks, Brian Dumas

Well, you could put them inside another class, and have a function in THAT
class which calls all three objects' function. But that doesn't save any
lines of code, unless you're making these same three calls in multiple
places.

Another idea is to have the pointers in an array, and loop through the
array. Again, that doesn't save much, unless you have more than just three
objects.

Perhaps you could tell us WHY you want to combine the calls? Writing three
lines of code is not that much work, after all.

-Howard
 
B

Brian Dumas

To explain it further, I have a class that has alot of functions, not just a
few. There might be up to 64 of the these objects in a users programs. In
some cases, they use the objects individually, but in some cases, they are
grouped together. So, for object 0, they might call function A with
parameter aaa, but they might call functiion a for objects 1,2,3,4 with
parameter bbb. In the case of object 0, it is one line. In the case of
objects 1,2,3,4, it is a total of 4 lines. And this all might be repeated
100 times in the users code. So, instead of these 4 lines everytime, it
would be great to group them into one object, and then just call that one
object 100 times. Anyway, I implemented it using STL and a duplicated the
function names in the primitive object to the grouped object, and they can
create grouped objects (even if it is just a group of 1). It just seems to
be alot of duplication.

Thanks, Brian
 
L

lilburne

Brian said:
To explain it further, I have a class that has alot of functions, not just a
few. There might be up to 64 of the these objects in a users programs. In
some cases, they use the objects individually, but in some cases, they are
grouped together. So, for object 0, they might call function A with
parameter aaa, but they might call functiion a for objects 1,2,3,4 with
parameter bbb. In the case of object 0, it is one line. In the case of
objects 1,2,3,4, it is a total of 4 lines. And this all might be repeated
100 times in the users code. So, instead of these 4 lines everytime, it
would be great to group them into one object, and then just call that one
object 100 times. Anyway, I implemented it using STL and a duplicated the
function names in the primitive object to the grouped object, and they can
create grouped objects (even if it is just a group of 1). It just seems to
be alot of duplication.

Indeed it does seem like a lot of code.

If they are grouped together why not put them into a
container and apply a functor to the objects in the container?
 
J

Jeff Schwab

Brian said:
This is a little long, sorry.
Let's say I have a class

class TMyClass
{
void funca();
void funcb();
};

and then I have objects

TMyClass* A = new TMyClass;
TMyClass* B = new TMyClass;
TMyClass* C = new TMyClass;

Sometimes, I need to call all three objects funca, like this :

A->funca();
B->funca();
C->funca();

So finally to the question - is there a way to combine these objects in a
"Meta" object that could do
something like this :
TMyClass* Meta=new TMyClass;
Meta->funca();

and have it run A, B, and C objects funca called?

THanks, Brian Dumas

Check out std::for_each, in the <algorithms> header.
 
S

Sharad Kala

Check out std::for_each, in the <algorithms> header.

ofcourse you meant <algorithm> without an 's' ;-)
Pointing it out just in case OP doesn't know of the correct header name.
 
J

Jeff Schwab

Sharad said:
ofcourse you meant <algorithm> without an 's' ;-)
Pointing it out just in case OP doesn't know of the correct header name.

Right you are!
 

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
474,161
Messages
2,570,891
Members
47,423
Latest member
henerygril

Latest Threads

Top