Struct Initialisation Syntax Help

 

Maybe a quick question, but I'm struggling to find the answer myself.

I'm trying to initialise all elements of an array within a struct at struct definition, currently using a constructor.

I can't get the right syntax though, or maybe I can't do it this way? - Can someone help?

struct myStruct
  {double            priceData[];
   bool              signal;
   double            magnitude;
                     myStruct() { priceData[]={-1}; }   //--- Constructor
  };


Thanks - I think I found a pretty good solution myself in the end, but it may be longwinded so any additional thoughts would be great!

New code...

struct myStruct
  {
   double            priceData[];
   bool              signal;
   double            magnitude;
      //--- Constructor
   myStruct() {
   ArrayResize(priceData,1440,1440);
   ArraySetAsSeries(priceData,true);
   for(int i=0; i<1440; i++){priceData[i]=-1;};
   }
  };

Probably no need to SetAsSeries at this point?

 
   for(int i=0; i<1440; i++){priceData[i]=-1;};

There's also ZeroMemory and ArrayFill.

 
lippmaje:

There's also ZeroMemory and ArrayFill.

Of course, I'm not thinking right...
I shall give that a test - my actual structures are more complicated still, but fingers crossed ZeroMemory will probably do what I want and I can ArrayFill the rest maybe!

Sorry for being dull - I've used these functions before, it just hadn't occured to me to use them in a constructor...
I'm just getting my head around OOP, but I guess the constructor is just code that runs whenever one of these structs is created and can contain any of these functions and more...

Thanks for the help.

 
mlbarnes:

Of course, I'm not thinking right...
I shall give that a test - my actual structures are more complicated still, but fingers crossed ZeroMemory will probably do what I want and I can ArrayFill the rest maybe!

Sorry for being dull - I've used these functions before, it just hadn't occured to me to use them in a constructor...
I'm just getting my head around OOP, but I guess the constructor is just code that runs whenever one of these structs is created and can contain any of these functions and more...

Thanks for the help.

ZeroMemory does a pretty nice job, but it seems to reset some of my enumerated data fields to zero, others oddly to -1... can't tell why yet, but I'm experimenting...
<Experiments demonstrated that I am an idiot - it does reset them all to zero (which if you define them correctly has the correct enumeration!), which is exactly what I need.

I should add that my actual struct constructions are far more complicated than the example I gave above!

I'm still wondering if I might like some more specific way to both initialise and reset a complex structure...

I think I shall write a function to reset everything as I would want it - just like the constructor in the first place.
Which begs the question - is there some way to re-run the constructor code on a struct even after it has been initialised and used etc?
Does a struct definiton somehow work like a class? Can I make the constructor public? How then to call it and point it at the correct already constructed struct?

Maybe easier to just copy paste the 'reset code' into a new function, but I'm trying to learn stuff!

 
You could build a 'Reset' function and call that from outside and from your constructor.
 
lippmaje:
You could build a 'Reset' function and call that from outside and from your constructor.
Sounds simple enough, thanks for helping me think it through.
One question - if I call it from the constructor, which could be creating any number of different new variables, how do I reference the 'target variable'?
 

'Target variable', what's that. Take a basic OO programming lession before going on. 

The Reset function will be part of the struct scope and so has access to all members, of course.

Reason: