Click to See Complete Forum and Search --> : Constructor with structs


Luc484
February 8th, 2008, 12:05 PM
Hi! I defined an array of structs this way:

mystruct name[10];

I saw this calls the constructor with no parameters, which needs to be defined. But I need to call another constructor with some parameters. Is it possible to call it on each element of the array after I created them, using a loop, for instance?
Thanks!

Paul McKenzie
February 8th, 2008, 12:18 PM
But I need to call another constructor with some parameters. Is it possible to call it on each element of the array after I created them, using a loop, for instance?
Thanks!If you mean by "it" the constructor, no. You would need to call another function, maybe called Init() that sets the members in a loop.

Instead of arrays, you can use a vector, which does what you want:

#include <vector>
#include <iostream>

struct mystruct
{
mystruct(int x) : nx(x) {}
int nx;
};

std::vector<mystruct> name(10, 4); // 10 mystructs, initialized using the int constructor

int main()
{
for (int i = 0; i < name.size(); ++i )
std::cout << name[i].nx << "\n";
}

This shows that all of the name.nx values are 4.

Regards,

Paul McKenzie

Luc484
February 8th, 2008, 01:05 PM
Oh, it's not possible... that's why I was not able to find it anywhere!

I'm asked to use a simple array, so I cannot use the vector class. And I'm asked to complete a given constructor with an unknown number of parameters (...). I mean that I'm given a draft of the structure with this constructor which has to be completed:

structname(...) ...

So maybe I simply have to create a constructor with 0 parameters. I can't think of anything else...

Thanks again!

0xC0000005
February 8th, 2008, 01:15 PM
Someone correct me if this is not legal, but you could try this:

mystruct name[10] = { mystruct(0), mystruct(1), mystruct(2), ... };

Luc484
February 8th, 2008, 03:48 PM
Unfortunately I have a case where the number of elements is 100. I don't think this method would not be good...

Paul McKenzie
February 8th, 2008, 05:17 PM
I'm asked to use a simple array, so I cannot use the vector class.Sorry for the rant, but I wonder who is giving these instructions, not just to you but to a lot of other posters? It seems every 10 or so posts says "I can't use this" or "I can't use that". No wonder more and more programmers are going to Java or C# -- they don't have to put up with this nonsense.

Back to the problem -- you are using a primitive type called an array. It is limited to what it can do with respect to initialization. If you want to construct a contiguous set of objects, and also call the non-default constructor, you must use something else, like a vector.

Regards,

Paul McKenzie

Luc484
February 8th, 2008, 05:35 PM
:-)
You're right but I'm attending a course of programming, and it seems I have to use only those structures which have been explained before the exercise was given. I have no problems in doing things like this, but I was wondering if it was possible to do it with particular restrictions. Otherwise it was quite simple to do it.

Thanks for the answer! And... be patient :-).