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::permissions_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 :D
Many thanks
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
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.
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
Re: Initialising an array of struct with values
Quote:
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. ;)
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