【发布时间】:2019-04-09 03:19:01
【问题描述】:
所以我可以通过引用传递,并将该引用存储在结构或类中,如果我在其他地方进行更改并再次检查存储它的引用,更改将在那里,因为我只是访问相同的内存。
有没有图书馆可以让我做这样的事情:
int foo = 9;
int bar = 5;
// obviously other arithmetic would exist too, and could be combined
Equation foo_minus_bar = Subtract(foo, bar);
// output: 4
cout << foo_minus_bar << endl;
foo = 11;
// output: 6
cout << foo_minus_bar << endl;
如果我可以访问输入(最好是平面数组或类似的,但乞丐不能是选择者,甚至可能是这样的),那也很好:
// literal character for character output: foo - bar
cout << foo_minus_bar.formula() << endl;
我可以自己制造一个,但如果有轮子,我宁愿不重新发明。
【问题讨论】:
-
有点像......哦。您实际上是在尝试将值“绑定”到您的函数并为其创建引用。我会让一个更好的 C++ 专家回答这个问题。首先:cplusplus.com/reference/functional/bind
-
似乎是一个解决方案,尽管结果可能看起来比我希望的更“hacky”!我什至会用 lambdas 试试看:)
标签: c++ pass-by-reference