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;
elseint &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?
int x = 23;
if (some condition)
int &intRef1 = 50 - x;
elseint &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.
struct conditional_ref
{
conditional_ref( bool condition, int& value, int minuend_if_true )
: value(value), condition(condition), minuend_if_true(minuend_if_true) {}
operatorint() const { return condition ? minuend_if_true - value : value ; }
conditional_ref& operator= ( int val )
{ value = condition ? minuend_if_true - val : val ; return *this ; }
int& value ;
constbool condition ;
constint 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;
}