Store values in int variables with equations

Hi, I'm fairly new to C++ (have only used the language for a few years) and I am not sure how (or even if) there is a way of doing something similar to this:

1
2
3
4
5
6
7
int x = 23;
if (some condition)
    int &intRef1 = 50 - x;
else
    int &intRef1 = x;
std::cout << intRef1; // would print out 27 if some condition = true
intRef1 = 6 // would store 44 in x if some condition = true 


I know my code does not work (or I at least strongly suspect it doesn't work, or doesn't work how I want it to). My question is: is there a way to do something like this, without tens or hundreds of lines of code, or a modification I can make to this code so that I can do something like this without putting an if statement every time I want to read or write from/to x?

Please tell me if question is too confusing.
Last edited on
could you please specify what is that you want to do ... in plain english?
In your example

1
2
3
4
5
6
7
int x = 23;
if (some condition)
    int &intRef1 = 50 - x;
else
    int &intRef1 = x;
std::cout << intRef1; // would print out 27 if some condition = true
intRef1 = 6 // would store 44 in x if some condition = true   



1) You may not define non-constant reference to a temporary unnamed expression. So this statement

int &intRef1 = 50 - x;

shall not be compiled.

2) All variables declared in if or else statements has block scope of these if and else statements. So hey will be destroyed after exiting from if and else statements. So this statement

std::cout << intRef1; // would print out 27 if some condition = true

shall not be compiled because variable intRef1 will not exist outside the scopes of if and else statements.
Last edited on
For the specific case, something like this would do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
struct conditional_ref
{
     conditional_ref( bool condition, int& value, int minuend_if_true )
        : value(value), condition(condition), minuend_if_true(minuend_if_true) {}

     operator int() const { return condition ? minuend_if_true - value : value ; }

     conditional_ref& operator= ( int val )
     { value = condition ? minuend_if_true - val : val ; return *this ; }

     int& value ;
     const bool condition ;
     const int minuend_if_true ;
};

int main()
{
    int x = 23 ;
    conditional_ref ref( true, x, 50 ) ;
    std::cout << x << ' ' << ref << '\n' ; // 23 27

    ref = 6 ;
    std::cout << x << ' ' << ref << '\n' ; // 44 6
}


You would wand to generalize this though. Perhaps with int being replaced with a generic type T and the evaluation of the condition and computation (subtract etc) of values specified using polymorphic call wrappers.
Ok, thanks for the replies, @JLBorges that solution looks ok, but may be a little overcomplicated for what I need. I've looked back through what I want my section of code to do, and have found that I only need a readable/writable variable that either writes 2-num or num to a variable (say x).

The reason for this is that I have an array of 3 variables (like myClass array[3]) that I sometimes want to read backwards (using the index 2-num, instead of num).

This would probably be a good problem to use a function for? Like:
x = assignX(num, boolResut)
and
1
2
3
4
int assignX(int num, bool blRes)
{
    return blRes ? 2-num : num; // Returns 2-num (if true) or num (if false)
}


Some related questions:
- Should this be declared inline?
- Can I pass a reference variable by reference to a function (not for this function, but for another function in my code?
i.e.
1
2
3
4
5
6
7
int variable;
int& ref1 = var;
// then pass ref1 to a function by ref
void f(int& ref2)
{
    ref2 = 9;
}

- Will that code set variable to 9?

Thanks in advance
Last edited on
> I have an array of 3 variables (like myClass array[3])
> that I sometimes want to read backwards (using the index 2-num, instead of num).

Just use a std::reverse_iterator<> http://en.cppreference.com/w/cpp/iterator/reverse_iterator

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iterator>
#include <iostream>

int main()
{
    enum { N = 10 } ;
    int array[N] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } ;
    std::reverse_iterator<int*> reverse_array( array + N ) ;

    for( int i=0 ; i < N ; ++i ) std::cout << array[i] << ' ' ;
    std::cout << '\n' ; // 0 1 2 3 4 5 6 7 8 9

    for( int i=0 ; i < N ; ++i ) std::cout << reverse_array[i] << ' ' ;
    std::cout << '\n' ; // 9 8 7 6 5 4 3 2 1 0

    array[2] = -99 ;
    reverse_array[2] = -555 ;
    for( int i=0 ; i < N ; ++i ) std::cout << array[i] << ' ' ;
    std::cout << '\n' ; // 9 8 -99 6 5 4 3 -555 1 0
}



> Can I pass a reference variable by reference to a function (not for this function, but for another function in my code?

Yes, of course.

Thanks heaps, that will do exactly what I wanted to do.
Topic archived. No new replies allowed.