I am doing this learning/practicing with arrays of objects and array of pointers to objects and I'm very confused about how to get dynamic arrays sizes.
I've got this:
private:
Client ** arr_client;
public:
static string members [];
then populated the array arr_client with tokenized data from the following static string array members:
static string members[] = {"Jhon Perez 623 22 12 1998"
,"Louis Smith 199 02 12 1988"
,"Daniel Martinez 106 02 01 2010"};
void load(void){
arr_client = new Client * [(sizeof(members)/sizeof(string))*sizeof(Client)];
for (i = 0; i < (sizeof(members)/sizeof(string)); i++){
istringstream stream(members[i],ios_base::in);
stream >> name;
stream >> lastname;
stream >> aux;
id = atoi(aux.c_str());
stream >> date;
date.append(" ");
stream >> aux;
date.append(aux);
date.append(" ");
stream >> aux;
date.append(aux);
arr_client[i] = new Client(name,lastname,id,date);
}
}
now, after the object array is full, I want to loop through arr_client but I cant seem to find the way to do it.
should I use:
for (int i =0; i < (sizeof(**arr_client)/sizeof(client)); i++)
or take the previous cal for size and do:
for (int i开发者_StackOverflow =0; i < (sizeof(members)/sizeof(string)); i++)
and be done with it? doesn't look that usefull to me... but then again I'm very new.
Few questions I've also got:
What if I don't know the size of that dynamically assinged array of objects? like in this case that im building upon a known sized array.
How can count how many elements are in it so I could loop through it?
Could I use an std::iterator for arr_client?
Any tips would be much appreciated =)
and.... yes, I do know about <vector>
and it's advantages but never hurts to know about these scenarios.
First, I applaud you for trying to learn the fundamentals before adopting an advanced tool like vector
.
There's no clean and reliable way to do what you ask. People usually remember the size of an array in a separate variable, like this:
unsigned int num_strings=3;
static string members[num_strings];
If you really want to determine the size of an array at runtime, you can do it with a template:
template<typename T, size_t N>
size_t arraySize( T(&)[N] )
{
return(N);
}
What if I don't know the size of that dynamically assinged array of objects? like in this case that im building upon a known sized array.
How can count how many elements are in it so I could loop through it?
You always need to know the size. You have to save it alongside the array, or you can't use it safely. That's why you should not use dynamic arrays, but rather safe containers like std::vector
or Boost.MultiArray.
And you cannot use sizeof
for that, as sizeof
is a compile-time operator.
Could I use an std::iterator for arr_client?
You can use pointer_to_start
as a range start and pointer_to_start + size
as a range end whenever an iterator is expected.
What if I don't know the size of that dynamically assinged array of objects? like in this case that im building upon a known sized array. How can count how many elements are in it so I could loop through it?
You can't. A pointer doesn't hold this information. You should keep it in some other way.
Could I use an std::iterator for arr_client?
No. std::iterator
is a template which you should derive your custom iterator class from to avoid retyping a lot of typedefs. But any pointer is a ready-to-use random access iterator.
I suggest you read a good C++ book. My own vote goes for Lippmann's C++ Primer. It will help you a real lot
精彩评论