Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependent variables in 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++?

like image 496
john smith Avatar asked Jul 29 '11 05:07

john smith


People also ask

What is dependent variable and example?

It is something that depends on other factors. For example, a test score could be a dependent variable because it could change depending on several factors such as how much you studied, how much sleep you got the night before you took the test, or even how hungry you were when you took it.

What is dependent variable in programming?

In computing, a dependent variable is any variable whose value, output or functioning depends on two or more independent variables. A dependent variable is used in computer programming to represent a value, process, function or entity within the programming paradigm/context/architecture.

Is C or Y the dependent variable?

y is often the variable used to represent the dependent variable in an equation.


2 Answers

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.

like image 111
Don Reba Avatar answered Oct 23 '22 09:10

Don Reba


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

like image 38
Chris W. Avatar answered Oct 23 '22 10:10

Chris W.