C++ 中的因变量?

发布于 2024-11-26 19:28:54 字数 899 浏览 0 评论 0原文

我之前问过,但不太清楚,所以我重新问一下。

我想要一个变量依赖于另一个变量的值,就像本例中的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

表情可笑 2024-12-03 19:28:54

您需要的是一个闭包。如果您可以使用 C++ 0x 功能,那么您很幸运。否则,您可以手动定义一个:

#include <iostream>
using namespace std;
struct B
{
    const int & a;

    B(const int & a) : a(a) {}

    // variable syntax (Sean Farell's idea)
    operator int () const { return a + 1; }

    // function syntax
    int operator () () const { return a + 1; }
};
int main()
{
    int a;
    B b(a);
    a = 3;
    cout << b << '\n'; // variable syntax
    a = 4;
    cout << b() << '\n'; // function syntax
}

您也可以在 main 中定义 B,但有些编译器不喜欢它。

C++ 0x lambda 语法如下所示

auto b = [&]() { return a + 1; }

: ] 表示 lambda 通过引用捕获局部变量。

What you need is a closure. If you can use C++ 0x features, you are in luck. Otherwise, you can define one manually:

#include <iostream>
using namespace std;
struct B
{
    const int & a;

    B(const int & a) : a(a) {}

    // variable syntax (Sean Farell's idea)
    operator int () const { return a + 1; }

    // function syntax
    int operator () () const { return a + 1; }
};
int main()
{
    int a;
    B b(a);
    a = 3;
    cout << b << '\n'; // variable syntax
    a = 4;
    cout << b() << '\n'; // function syntax
}

You can also define B inside main, but some compilers would not like it.

The C++ 0x lambda syntax looks like this:

auto b = [&]() { return a + 1; }

The [&] means that the lambda captures local variables by reference.

清晨说晚安 2024-12-03 19:28:54

如果您使用的是 C++0x(GCC 4.5+、Visual C++ 2010),则可以使用 lambda:

int a = 5;
auto b = [&a]{ return a + 1; };

std::cout << b() << std::endl;

不过,根据您正在做的事情,可能有更干净的解决方案 - 可能是经典“方法”的一些变体在'a'中并返回'b'”

If you're using C++0x (GCC 4.5+, Visual C++ 2010), you can use lambdas:

int a = 5;
auto b = [&a]{ return a + 1; };

std::cout << b() << std::endl;

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'"

苦妄 2024-12-03 19:28:54

您可以定义一个具有成员 a 的类,然后定义一个返回 a+1 值的函数 b()。基本的实现类似于:

class Dependent {
public:
    Dependent(void) { m_value = 0; }
    void set(int value) { m_value = value; }
    int b(void) { return(m_value + 1); }
private:
    int m_value;
};


int main(){
    Dependent a;
    a.set(3);
    cout << a.b();
    a.set(4);
    cout << a.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:

class Dependent {
public:
    Dependent(void) { m_value = 0; }
    void set(int value) { m_value = value; }
    int b(void) { return(m_value + 1); }
private:
    int m_value;
};


int main(){
    Dependent a;
    a.set(3);
    cout << a.b();
    a.set(4);
    cout << a.b();
}

You could add operator overloading as appropriate to make it work more like normal integers if you so desired.

嘿嘿嘿 2024-12-03 19:28:54

如果您使用 lambda 函数 (c++0x),这是可能的,因为它们可以捕获局部变量。

示例:

int main()
{
  int a;
  auto f = [&] () -> int { return a + 1; };
  a = 3;
  std::cout << f() << std::endl;
  a = 4;
  std::cout << f() << std::endl;
  return 0;
}

结果:(

4
5

参见 http://ideone.com/MlzX7 获取证明)

This is possible if you use lambda functions (c++0x), because they can capture local variables.

Example:

int main()
{
  int a;
  auto f = [&] () -> int { return a + 1; };
  a = 3;
  std::cout << f() << std::endl;
  a = 4;
  std::cout << f() << std::endl;
  return 0;
}

Result:

4
5

(See http://ideone.com/MlzX7 for proof)

紧拥背影 2024-12-03 19:28:54

一种简单的方法是使用预处理器宏,但没有任何 C++ 特定的宏:

#define b ((a)+1)

int main(){
    int a;
    a=3;
    cout << b;
    a=4;
    cout << b;
}

#undef b

A simple approach is to use pre-processor macros, nothing C++ specific about it though:

#define b ((a)+1)

int main(){
    int a;
    a=3;
    cout << b;
    a=4;
    cout << b;
}

#undef b
可可 2024-12-03 19:28:54

你可以使用 C++0x 吗?如果是,

int main()
{
    int a = 10;
    auto b = [&a]() -> int { return a + 1; };
    cout << b() << endl;
}

由于它没有用 c++0x 标记,因此您可以使用嵌套类而不是嵌套函数。 Herb sutter 的这篇专栏将为您提供现有的 C++ 帮助。 http://www.gotw.ca/gotw/058.htm

Are you OK using C++0x ? if yes,

int main()
{
    int a = 10;
    auto b = [&a]() -> int { return a + 1; };
    cout << b() << endl;
}

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

层林尽染 2024-12-03 19:28:54

上面的方法不起作用,因为 C++ 不允许嵌套函数。

您可以使用嵌套结构来模拟它。在 C++0x 中,您可以使用 lambda 函数,它提供了与函数内函数相同的方法。

The above doesn't work because C++ doesn't allow nested functions.

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.

﹏半生如梦愿梦如真 2024-12-03 19:28:54

定义一个名为 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.

や莫失莫忘 2024-12-03 19:28:54

我想要一个变量依赖于另一个变量的值
变量,如本例中的 b:

我发现你只需要一个引用变量:

int a;
int &b =a;
a=10;
cout << b; // 10

为什么 C++0x lambda 会为此出现,我不明白。

I want to have a variable that depends on the value of another
variable, like b in this example:

I see you just need a reference variable:

int a;
int &b =a;
a=10;
cout << b; // 10

Why C++0x lambdas do come for this, I dont understand.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文