|
-
July 29th, 2010, 10:33 AM
#1
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
-
August 2nd, 2010, 02:42 AM
#2
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
-
August 2nd, 2010, 11:05 AM
#3
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
-
August 4th, 2010, 03:47 AM
#4
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
-
August 4th, 2010, 04:17 PM
#5
Re: Initialising an array of struct with values
 Originally Posted by TheGrovesy
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
-
August 12th, 2010, 01:35 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|