How can I make a variable always equal to the result of some calculations?

前端 未结 12 1775
傲寒
傲寒 2021-02-02 05:34

In math, if z = x + y / 2, then z will always change whenever we replace the value of x and y. Can we do that in programming

12条回答
  •  长情又很酷
    2021-02-02 06:10

    The closest you probably can get is to create a functor:

    #include 
    
    int main() {
        int x;
        int y;
    
        auto z = [&x, &y] { return x + y; }; // a lambda capturing x and y
    
        while(true) {
            std::cin >> x;
            std::cin >> y;
            std::cout << z() << "\n";
        }
    }
    

提交回复
热议问题