Storing variable number of data in a class

S

Snyke

Well my problem is simple:
I have a class which has a variable number of members. Lets say I have a
class Family and a variable number of FamilyMembers. Until now I used a
member of Family called members of type List<FamilyMembers> since an
array is unsuitable for the task (unless I'm much mistaken array sizes
can't be modified on runtime, can they?). Now I'm asking myself if there
isn't a better way to handle this problem, since it's a very common problem.

Thanks.
 
P

Peter van Merkerk

Snyke said:
Well my problem is simple:
I have a class which has a variable number of members. Lets say I have a
class Family and a variable number of FamilyMembers. Until now I used a
member of Family called members of type List<FamilyMembers> since an
array is unsuitable for the task (unless I'm much mistaken array sizes
can't be modified on runtime, can they?).

The std::vector class is in many cases an excellent alternative for a C
style array, and can be resized during runtime. You can access elements
in a vector just like an array. And operations like adding and removing
elements work just like the std::list class you already know.
Now I'm asking myself if there
isn't a better way to handle this problem, since it's a very common
problem.

What is the problem your having with using the standard container classes?
 
M

Mark

Snyke said:
Well my problem is simple:
I have a class which has a variable number of members. Lets say I have a
class Family and a variable number of FamilyMembers. Until now I used a
member of Family called members of type List<FamilyMembers> since an
array is unsuitable for the task (unless I'm much mistaken array sizes
can't be modified on runtime, can they?). Now I'm asking myself if there
isn't a better way to handle this problem, since it's a very common
problem.

Thanks.

Using dynamic arrays you can resize arrays at runtime. But a better
alternative is using the vector class from the standard template
library. For more info, you can look at:
http://www.icce.rug.nl/documents/cplusplus/

--
Mark Moonen
http://perswww.kuleuven.ac.be/Mark_Moonen/

Experience is a dear teacher, and only fools will learn from no other.
Benjamin Franklin
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

Snyke said:
Well my problem is simple:
I have a class which has a variable number of members. Lets say I have a
class Family and a variable number of FamilyMembers. Until now I used a
member of Family called members of type List<FamilyMembers> since an
array is unsuitable for the task (unless I'm much mistaken array sizes
can't be modified on runtime, can they?). Now I'm asking myself if there
isn't a better way to handle this problem, since it's a very common
problem.
std::list,std::vector and friends are just what you want here, so you
seem to be on the right track. You might store pointers to them though,
if a familymemeber should be manipulated outside the Family class. Though
it makes allocation and deallocation a bit trickier.
 
S

Snyke

Peter said:
What is the problem your having with using the standard container classes?
It's not the fact that it's a standard container class that disturbs me,
but the messy iterator work when using lists :)
 
S

Snyke

Nils said:
std::list,std::vector and friends are just what you want here, so you
seem to be on the right track. You might store pointers to them though,
if a familymemeber should be manipulated outside the Family class. Though
it makes allocation and deallocation a bit trickier.
For encapsulation matters I use instances and not pointers to them,
since the Design should be good enough to encapsulate the whole thing
or, if not, provide access functions to the member variable :)
 
J

Jeff Flinn

Snyke said:
It's not the fact that it's a standard container class that disturbs me,
but the messy iterator work when using lists :)

Messy?

std::vector uses iterators as well.

Jeff
 
P

Peter van Merkerk

Snyke said:
It's not the fact that it's a standard container class that disturbs me,
but the messy iterator work when using lists :)

I see, fortunately the elements in a vector can be accessed just like the
elements in an array :)
Iterators may require some getting used to, but being able to iterate over
elements the same way regardless of the container is a very elegant and
powerful concept. But I understand what you mean with the "messy" part. :)
 

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

No members online now.

Forum statistics

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

Latest Threads

Top