Can I access an object in C ++ other than using an expression?

According to C ++ 03 3.10 / 1, each expression is either an lvalue value or an r value. When I use = to assign a new value to a variable, the variable name to the left of the assignment is an lvalue expression. And it looks like everything I'm trying to do with a variable will still use some kind of expression.

Is there a way to manipulate a variable in C ++ differently than using an expression?

+7
source share
3 answers

The only way is through an instruction, but not through an expression that is part of such a statement. An example is the definition of std::string x; . This calls the default ctor on x . But is this considered manipulation for you?

Actually there are not many other statements. Loop control statements cannot modify the objects themselves, except through the side effects of loop control expressions. goto , break and continue cannot do this at all. throw is an expression, and catch() cannot change anything, so the pair doesn't matter either. I do not think there is another non-expression.

+2
source

You can set the value of a variable without using an expression, but you cannot choose what value it gets. The way I read C ++ 11 standard application A (language grammar), ad not an expression. If you write int a; in the function area, a will be assigned an undefined value. If you write it in the file area, a will be set to 0. But you cannot assign arguments to the constructor value or pass, because the result of this is an expression.

+2
source

Not sure if it strictly answers your question, but you can indirectly manipulate the variable. For example:.

 int a; int *pA = &a; *pA = 5; 

Here the value of a changes, but without an expression involving a . The expression includes only pA .

In addition, there may be side effects of unrelated operations that result in a variable change, intentional or not (for example, memory corruption that unintentionally changes a variable).

0
source

All Articles