C++ 中的因变量?
我之前问过,但不太清楚,所以我重新问一下。
我想要一个变量依赖于另一个变量的值,就像本例中的 b 一样:
int main(){
int a;
dependent int b=a+1; //I'm just making this up
a=3;
cout << b; //prints 4
a=4;
cout << b; //prints 5
}
当然,这在 C++ 中不存在,但这就是我想要的。
因此,我尝试创建一个函数:
int main(){
int a;
int b(){ return a+1; } //error
a=3;
cout << b(); //would print 4 if C++ allowed nested functions
a=4;
cout << b(); //would print 5 if C++ allowed nested functions
}
上面的方法不起作用,因为 C++ 不允许嵌套函数。
我只能在 main() 之外创建函数,如下所示:
int b(){
return a+1; //doesn't work because a is not in scope
}
int main(){
int a;
a=3;
cout << b();
a=4;
cout << b();
}
但这不起作用,因为 a 与 b() 不在同一范围内,所以我必须将 a 作为参数传递,但我不想这样做那。
有没有什么技巧可以得到类似于 C++ 中因变量的东西?
I tried asking before but I wasn't very clear so I'm re-asking it.
I want to have a variable that depends on the value of another variable, like b in this example:
int main(){
int a;
dependent int b=a+1; //I'm just making this up
a=3;
cout << b; //prints 4
a=4;
cout << b; //prints 5
}
Of course, this does not exist in C++, but this is what I want.
So instead I tried making a function:
int main(){
int a;
int b(){ return a+1; } //error
a=3;
cout << b(); //would print 4 if C++ allowed nested functions
a=4;
cout << b(); //would print 5 if C++ allowed nested functions
}
The above doesn't work because C++ doesn't allow nested functions.
I can only make functions outside of main(), like this:
int b(){
return a+1; //doesn't work because a is not in scope
}
int main(){
int a;
a=3;
cout << b();
a=4;
cout << b();
}
But this does not work because a is not in the same scope as b(), so I would have to pass a as a parameter and I don't want to do that.
Are there any tricks to get something similar to a dependent variable working in C++?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您需要的是一个闭包。如果您可以使用 C++ 0x 功能,那么您很幸运。否则,您可以手动定义一个:
您也可以在
main
中定义B
,但有些编译器不喜欢它。C++ 0x lambda 语法如下所示
: ] 表示 lambda 通过引用捕获局部变量。
What you need is a closure. If you can use C++ 0x features, you are in luck. Otherwise, you can define one manually:
You can also define
B
insidemain
, but some compilers would not like it.The C++ 0x lambda syntax looks like this:
The
[&]
means that the lambda captures local variables by reference.如果您使用的是 C++0x(GCC 4.5+、Visual C++ 2010),则可以使用 lambda:
不过,根据您正在做的事情,可能有更干净的解决方案 - 可能是经典“方法”的一些变体在'a'中并返回'b'”
If you're using C++0x (GCC 4.5+, Visual C++ 2010), you can use lambdas:
Depending on what you're doing, though, there are probably cleaner solutions - possibly some variation of the classic "method that takes in 'a' and returns 'b'"
您可以定义一个具有成员 a 的类,然后定义一个返回 a+1 值的函数 b()。基本的实现类似于:
如果您愿意,您可以根据需要添加运算符重载,使其更像普通整数。
You could define a class that had a member a, and then a function b() that returned the value of a+1. A basic implementation would be something like:
You could add operator overloading as appropriate to make it work more like normal integers if you so desired.
如果您使用 lambda 函数 (c++0x),这是可能的,因为它们可以捕获局部变量。
示例:
结果:(
参见 http://ideone.com/MlzX7 获取证明)
This is possible if you use lambda functions (c++0x), because they can capture local variables.
Example:
Result:
(See http://ideone.com/MlzX7 for proof)
一种简单的方法是使用预处理器宏,但没有任何 C++ 特定的宏:
A simple approach is to use pre-processor macros, nothing C++ specific about it though:
你可以使用 C++0x 吗?如果是,
由于它没有用 c++0x 标记,因此您可以使用嵌套类而不是嵌套函数。 Herb sutter 的这篇专栏将为您提供现有的 C++ 帮助。 http://www.gotw.ca/gotw/058.htm
Are you OK using C++0x ? if yes,
Since, it is not tagged with c++0x, you can use nested classes instead of nested functions. This column from Herb sutter would help you for existing c++. http://www.gotw.ca/gotw/058.htm
您可以使用嵌套结构来模拟它。在 C++0x 中,您可以使用 lambda 函数,它提供了与函数内函数相同的方法。
You can simulate that using nested structure. In C++0x you can make use of lambda function, which provides the same means of function inside function.
定义一个名为 LinkedInt 的类或行为类似 int 的类,但其自身具有相关关系,并且还有一个附加成员,该成员是指向要在计算整数值时计算的函数的函数指针。非常简单。如果您需要一些编码方面的指导,请告诉我。
简而言之,OOP 足以解决这个问题。
Define a class called LinkedInt or something that behaves like an int, but has a RelatedTo relationship on itself and an additional member that is a function pointer to the function to evaluate when computing the integer's value. Pretty straightforward. Let me know if you need some pointers on the coding.
The short answer is that OOP is more than enough to bury this problem.
我发现你只需要一个引用变量:
为什么 C++0x lambda 会为此出现,我不明白。
I see you just need a reference variable:
Why C++0x lambdas do come for this, I dont understand.