CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2008
    Posts
    56

    Constructor with structs

    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!

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Constructor with structs

    Quote Originally Posted by Luc484
    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:
    Code:
    #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

  3. #3
    Join Date
    Feb 2008
    Posts
    56

    Re: Constructor with structs

    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!

  4. #4
    Join Date
    May 2002
    Posts
    1,435

    Re: Constructor with structs

    Someone correct me if this is not legal, but you could try this:
    Code:
    mystruct name[10] = { mystruct(0), mystruct(1), mystruct(2), ... };
    Last edited by 0xC0000005; February 8th, 2008 at 02:21 PM.

  5. #5
    Join Date
    Feb 2008
    Posts
    56

    Re: Constructor with structs

    Unfortunately I have a case where the number of elements is 100. I don't think this method would not be good...

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Constructor with structs

    Quote Originally Posted by Luc484
    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
    Last edited by Paul McKenzie; February 8th, 2008 at 06:20 PM.

  7. #7
    Join Date
    Feb 2008
    Posts
    56

    Re: Constructor with structs

    :-)
    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 :-).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured