Here is one way to do it - the benefit is you can also call the trade_record_Init() function directly too, later with new arguments if the values need to be changed
struct trade_record { double sl; double tp; double longVolume; double shortVolume; bool issue; string message; trade_record() //constructor { this.trade_record_Init(10, 10); }; void trade_record_Init(double _sl, double _tp) { this.sl = _sl; this.tp = _tp; }; trade_record getTradeRecord() { trade_record result; return result; } };
struct trade_record { double sl; double tp; double longVolume; double shortVolume; bool issue; string message; trade_record() : sl (NULL), tp (NULL), longVolume (NULL), shortVolume (NULL), issue (NULL), message (NULL) {}; trade_record(const trade_record& p_in) { sl = p_in.sl; tp = p_in.tp; longVolume = p_in.longVolume; shortVolume = p_in.shortVolume; issue = p_in.issue; message = p_in.message; }; trade_record(double _sl, double _tp) { this.sl = _sl; this.tp = _tp; }; trade_record operator=(const trade_record& p_in) { sl = p_in.sl; tp = p_in.tp; longVolume = p_in.longVolume; shortVolume = p_in.shortVolume; issue = p_in.issue; message = p_in.message; return(this); }; };
You raise a good point about having other constructors - then the fix to the original code is easy, just adding 1 line to make a default constructor.
struct trade_record { double sl; double tp; double longVolume; double shortVolume; bool issue; string message; trade_record() {}; //Default constructor fix added trade_record(double _sl, double _tp) { this.sl = _sl; this.tp = _tp; }; }; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ trade_record getTradeRecord() { trade_record result(10, 10); return result; }
I was precise - I compiled your code as shown below before responding and unlike mine, it does not work (but I decided to be polite and not mention that).
Of course I can fix your code too, but it is better you post working code in the first place - should we say OOPs? Just kidding of course.
My second post was to acknowledge you had thrown light on another (purist) way to solve this, read it as you will.
struct trade_record { double sl; double tp; double longVolume; double shortVolume; bool issue; string message; trade_record() : sl (NULL), tp (NULL), longVolume (NULL), shortVolume (NULL), issue (NULL), message (NULL), {}; trade_record(const trade_record& p_in) { sl = p_in.sl; [...] }; trade_record(double _sl, double _tp) { this.sl = _sl; this.tp = _tp; }; trade_record operator=(const trade_record& p_in) { sl = p_in.sl; [...] return(this); }; };
trade_record(const trade_record& p_in)
{
sl = p_in.sl;
[...]
};
You did say "Be precise" so why not follow your own advice?
And if we are being precise, have you observed that we are talking about a Struct and not an object? So where does that fit with your assertion about "scope of correct OOP"?
Well let's not prolong this debate when the solution is evident.
I see your point about using constructors and appreciate you are passionate about best practice. I too like this, although perhaps I am less worried about compliance with specific design ideas and paradigms - I just want well written and efficient code, and as we see in this case, I also prefer the simplest/quickest/best solution to resolve a problem at hand (which your involvement lead to... so thanks)
I agree it is not good to spoon feed too much, but when someone asks for help, I believe one should at least post compilable, relevant and working code. It saves time for anyone who wants to compile and test it, rather than having to guess what the provider meant and reverse engineer it to make it work.
Have a good day!
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
The goal is to, have a function getTradeRecord that returns a struct trade_record. That struct should be initialized by its constructor.
The problem is, when the struct trade_record is initialized by its constructor, we get a compilation error in the return statement.
Code bellow:
Image of the compilation error bellow follow as an attachment.
Is this a issue with the language, or I'm missing something?
Are there a more idiomatic why of doing this? Could someone provide some guidance in returning a record type from a function.