Dew*_*413 1 c++ algorithm function function-call chaining
我对从哪里开始解决这个问题有疑问。问题需要如下。
// We want to create a function that will add numbers together,
// when called in succession.
add(1)(2); // == 3
Run Code Online (Sandbox Code Playgroud)
我从未见过以这种方式使用函数,目前我不知道从哪里开始。此外,我尝试对参数链进行一些研究,但这就是我能找到的全部。
https://levelup.gitconnected.com/how-to-implement-method-chaining-in-c-3ec9f255972a
如果你们有任何疑问,我可以编辑我的代码或问题。任何帮助表示赞赏。
\n\n....方式,我目前不知道从哪里开始?
\n
一种方法是从匿名(未命名)函子\xe2\x9c\xb1开始,它具有operator(),返回对 的引用this,如下所示:
struct { // unnamed struct\n int result{ 0 };\n\n auto& operator()(const int val) noexcept\n {\n result += val;\n return *this; // return the instance itself\n }\n // conversion operator, for converting struct to an int\n operator int() { return result; }\n\n} add; // instance of the unnamed struct\n\n\nint main() \n{\n std::cout << add(1)(2); // prints: 3\n}\nRun Code Online (Sandbox Code Playgroud)\n\n\xe2\x9c\xb1在此处阅读有关未命名结构、函子和转换运算符的更多信息:
\n\n