Support Board
Date/Time: Mon, 24 Feb 2025 15:27:46 +0000
[User Discussion] - sc. structure passed by reference as default value to all functions
View Count: 1198
[2021-03-08 17:17:22] |
User30743 - Posts: 365 |
is there any way to pass sc structure to custom functions by &reference, and having it as a default value? here is what i mean.. For example I have a class like this namespace util{ class Logger { public: static void log(SCStudyInterfaceRef& sc, SCDateTime value, SCString comment = "") { SCString s = sc.FormatDateTime(value).GetChars(); comment += s; sc.AddMessageToLog(comment, 1); } static void log(SCStudyInterfaceRef& sc, const char* value, SCString comment = "") { comment += value; sc.AddMessageToLog(comment, 1); } static void log(SCStudyInterfaceRef& sc, int value, SCString comment = "") { SCString strVal = std::to_string(value).c_str(); comment += strVal; sc.AddMessageToLog(comment, 1); } }; The first argument is of course SCStudyInterfaceRef which I am passing via ref &. then the called needs to pass this argument sc SCSFExport scsf_LogUtilsTest(SCStudyInterfaceRef sc) { SCDateTime TradingDayStartDateTime = sc.GetTradingDayStartDateTimeOfBar(sc.BaseDateTimeIn[sc.IndexOfLastVisibleBar]); util::Logger::log(sc, TradingDayStartDateTime); util::Logger::log(sc, 3.23); } But, it is tedious to pass sc all the time everywhere. I would like to make it default in all my custom functions because it is obvious it will be passed there, there is no other option anyway But I did not figure out how to make it work. Any suggestions? Date Time Of Last Edit: 2021-03-08 17:20:28
|
[2021-03-08 18:39:40] |
User907968 - Posts: 833 |
is there any way to pass sc structure to custom functions by &reference, and having it as a default value?
No not in the way that you want, because you are passing a reference to a specific instance of sc structure/object that relates to the chart/study.Also 'SCStudyInterfaceRef' is already alias for 's_sc&' therefore additional '&' is not needed. typedef s_sc& SCStudyInterfaceRef;
Date Time Of Last Edit: 2021-03-08 18:41:09
|
[2021-03-09 18:04:53] |
User30743 - Posts: 365 |
so how do i make it that i will not have to pass sc everytime?
|
[2021-03-09 20:38:31] |
norvik_ - Posts: 106 |
namespace util{
class Logger { public: Logger(SCStudyInterfaceRef sc):p_Interface(&sc) static void log(const SCDateTime& value, SCString comment = "") { SCString s = p_Interface->FormatDateTime(value).GetChars(); comment += s; p_Interface->AddMessageToLog(comment, 1); } private: s_sc* p_Interface; }; or make a singleton. class Logger { public: static Logger& Instance(){static Logger l;return ;l} void Attach(SCStudyInterfaceRef sc){p_Interface = ≻} void log(const SCDateTime& value, SCString comment = "") { SCString s = p_Interface->FormatDateTime(value).GetChars(); comment += s; p_Interface->AddMessageToLog(comment, 1); } private: Logger(){} s_sc* p_Interface; }; and use: Logger::Instance().Attach(sc);
Logger::Instance().log(value,comment); Date Time Of Last Edit: 2021-03-09 20:42:24
|
[2021-03-10 08:59:55] |
User30743 - Posts: 365 |
wow what is this Logger(SCStudyInterfaceRef sc):p_Interface(&sc) ? Logger is inheriting from p_Interface(&sc)?and at the same time it is having s_sc* p_Interface; as private member?well, this is quite a new construct for me. I am still a c++ beginner so sorry is this is a dumb question.. can u explain me ? Date Time Of Last Edit: 2021-03-10 09:00:46
|
[2021-03-10 19:15:13] |
norvik_ - Posts: 106 |
Hi. No inheritance in my first sample. It is class constructor with the reference to the s_sc structure as constructor parameter, that is all. I think it is a not very good idea to use s_sc to inherit your own class from it, but you can try, I never had checked. As for private state of p_Interface, the only one reason, you will never use it outside a class, so simply no sense make it public. Date Time Of Last Edit: 2021-03-10 20:17:08
|
[2021-03-16 19:35:21] |
User30743 - Posts: 365 |
Logger(SCStudyInterfaceRef sc):p_Interface(&sc)
is a constructor with s_sc as parameter? Really strange. I have never seen anything like this in C++ before. And it cannot be compiled by the way. Isnt this just a totally invalid construct? Date Time Of Last Edit: 2021-03-16 19:53:48
|
[2021-03-16 21:54:19] |
User907968 - Posts: 833 |
Isnt this just a totally invalid construct?
No, just one form of initialization.https://en.cppreference.com/w/cpp/language/direct_initialization https://en.cppreference.com/w/cpp/language/list_initialization I found this book useful when starting out with c++ - https://www.stroustrup.com/programming.html Date Time Of Last Edit: 2021-03-16 21:59:31
|
[2021-03-17 12:31:31] |
User30743 - Posts: 365 |
ok, then why it cannot be compiled?
|
[2021-03-17 13:27:17] |
User907968 - Posts: 833 |
namespace util{ class Logger { public: Logger(SCStudyInterfaceRef sc):p_Interface(&sc) {} void log(const SCDateTime& value, SCString comment = "") { SCString s = p_Interface->FormatDateTime(value).GetChars(); comment += s; p_Interface->AddMessageToLog(comment, 1); } private: s_sc* p_Interface; }; } Cannot have static function that references non-static member, also add braces to constructor. |
[2021-03-17 19:19:31] |
User30743 - Posts: 365 |
oh, now I understand. thank u! is there a way to make it work also for static members? |
To post a message in this thread, you need to log in with your Sierra Chart account: