CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2010
    Posts
    5

    Question Initialising an array of struct with values

    I am from a C background and when creating lookup tables I always used to create a struct of the type i wanted and then create an array of that struct type. As this was a static table I could initialise the whole table when I declared it.

    I am trying to do the same in Managed C++ and it appears it is not a straight forward as good old C. The code I am trying, below, is giving me a compile time error saying:

    "error C2440: 'initializing' : cannot convert from 'int' to 'thales_esecurity::User:ermissions_s ^'"

    Code:
    // Example structure
    ref struct permissions_s
    {
       int one;
       int two;
       bool valid;
    };
    
    // Create static instance of structure and populate it
    static array <permissions_s^>^ permissions =
    {
       { 1, 1, true },
       { 1, 1, true },
       { 1, 2, false },
       { 2, 2, true }
    };
    Any ideas? please tell me what I'm doing wrong

    Many thanks

  2. #2
    Join Date
    Jul 2010
    Posts
    5

    Re: Initialising an array of struct with values

    So is this not actually possible? Doing further research and I still cannot find a solution which is making me think that this is not actually possible!

    Can anyone confirm/deny this?

    Thanks

  3. #3
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Initialising an array of struct with values

    Since your struct is a reference type, you can only instantiate it using gcnew. See http://msdn.microsoft.com/en-us/libr...h7(VS.80).aspx

    I don't think you can initialize a struct in C++/CLI as you can in C; you'll have to add a constructor to the struct.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  4. #4
    Join Date
    Jul 2010
    Posts
    5

    Re: Initialising an array of struct with values

    Thanks, i kind of came to that conclusion in the end, so have done exactly that. I still think the way you could do it in C was a little neater... oh well that's progress i suppose! lol

    Cheers

  5. #5
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Initialising an array of struct with values

    Quote Originally Posted by TheGrovesy View Post
    I still think the way you could do it in C was a little neater... oh well that's progress i suppose! lol
    If you think less expressiveness is neater.
    What if there is a precondition that one must be smaller than two? How would you enforce it in C? You can't. At least, not neatly.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  6. #6
    Join Date
    Aug 2010
    Posts
    51

    Thumbs up Re: Initialising an array of struct with values

    hi,

    I still think the way you could do it in C was a little neater... oh well that's progress i supposed

    thanks



    regards,
    phe9oxis,
    http://www.guidebuddha.com

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