lvalue

assigning to rvalue: why does this compile?

情到浓时终转凉″ 提交于 2019-12-01 17:49:21
问题 In the following example: class A { private: double content; public: A():content(0) {} A operator+(const A& other) { content += other.content; return *this; } void operator=(const A& other) { content = other.content; } }; A is a simple wrapper for a double for which the + and = operators have been overloaded. In the following use: int main(int argc, char *argv[]) { A a, b, c; (a+b) = c ; // Why is this operation legal? } Why does (a+b) = c compile? I would like to know why this statement is

lvalue and rvalue for pre/postfix increment

故事扮演 提交于 2019-12-01 16:45:36
Learning the lvalue and rvalue. The definition is whatever that can be "address of" is the left value and otherwise, it is rvalue. I checked the operator precedence, both prefix and postfix increment has higher priority than the "address of" operator. For the following two examples, can anyone explain a bit why the first one "&++value1" is a lvalue while the second one "&value1++" is a rvalue. My wrong understanding for both case is: pValue1 is pointing to the value1 variable. No matter value1 is changed to 8 before or after the address correlation is built, the value1 variable always occupies

Why are multiple increments/decrements valid in C++ but not in C?

我怕爱的太早我们不能终老 提交于 2019-12-01 16:24:15
问题 test.(c/cpp) #include <stdio.h> int main(int argc, char** argv) { int a = 0, b = 0; printf("a = %d, b = %d\n", a, b); b = (++a)--; printf("a = %d, b = %d\n", a, b); return 0; } If I save the above as a .cpp file, it compiles and outputs this upon execution: a = 0, b = 0 a = 0, b = 1 However, if I save it as a .c file, I get the following error: test.c:7:12: error: lvalue required as decrement operator. Shouldn't the (++a) operation be resolved before the (newValue)-- operation? Does anyone

lvalue and rvalue for pre/postfix increment

徘徊边缘 提交于 2019-12-01 15:28:07
问题 Learning the lvalue and rvalue. The definition is whatever that can be "address of" is the left value and otherwise, it is rvalue. I checked the operator precedence, both prefix and postfix increment has higher priority than the "address of" operator. For the following two examples, can anyone explain a bit why the first one "&++value1" is a lvalue while the second one "&value1++" is a rvalue. My wrong understanding for both case is: pValue1 is pointing to the value1 variable. No matter

What is the reasoning behind the naming of “lvalue” and “rvalue”?

筅森魡賤 提交于 2019-12-01 14:47:45
What is the reasoning behind the naming of "lvalue" and "rvalue" in C/C++ (I know how they function)? The standard mentions this: An lvalue (so called, historically, because lvalues could appear on the left-hand side of an assignment expression) [...] An rvalue (so called, historically, because rvalues could appear on the right-hand side of an assignment expression) [...] That is, an lvalue was something you could assign to and an rvalue was something you could assign from. However, this has gradually gotten further and further from the truth. A simple example of an lvalue that you can't

behaviour of const_cast

霸气de小男生 提交于 2019-12-01 12:39:36
I was reading about const_cast operator in c++ 1.First weird thing thing i can't understand is const_cast operator syntax i.e. -const_cast--<--Type-->--(--expression--)-------------------->< what i have understand about this syntax is that it helps to cast away constness of an expression of type Type .But consider this code class ConstTest { private: int year; public: ConstTest() : year(2007) {} void printYear() const; }; int main() { ConstTest c; c.printYear(); return 0; } void ConstTest::printYear() const { ConstTest *c = const_cast<ConstTest*>(this); c->year = 42; std::cout << "This is the

why lvalue required as increment operand error? [duplicate]

青春壹個敷衍的年華 提交于 2019-12-01 12:27:57
问题 This question already has answers here : lvalue required as increment operand error (4 answers) Closed 5 years ago . Why lvalue required as increment operand Error In a=b+(++c++); ? Just Wanted to assign 'b+(c+1)' to 'a' and Increment 'C' by 2 at the same time. I'M A Beginner Just Wanted A Clarification About What "LVALUE ERROR" Actually Is? main() { int a=1,b=5,c=3; a=b+(++c++); printf("a=%d b= %d c= %d \n",a,b,c); } 回答1: Postfix increment binds tighter than prefix increment so what you

behaviour of const_cast

不问归期 提交于 2019-12-01 10:03:28
问题 I was reading about const_cast operator in c++ 1.First weird thing thing i can't understand is const_cast operator syntax i.e. -const_cast--<--Type-->--(--expression--)-------------------->< what i have understand about this syntax is that it helps to cast away constness of an expression of type Type .But consider this code class ConstTest { private: int year; public: ConstTest() : year(2007) {} void printYear() const; }; int main() { ConstTest c; c.printYear(); return 0; } void ConstTest:

Why isn't the result of this cast an lvalue?

旧街凉风 提交于 2019-12-01 06:36:37
I need some advice with this strange behavior – lets have this code: int ** p; This compiles without any trouble: p++; But this: ((int**)p)++; Gives me this error message: “error: lvalue required as increment operand” . I am casting to p to the type it already is, nothing changes, so what is the problem? This is simplified version of problem I came across, when I was trying to compile one old version of gdb . So I suppose, that this worked and something changed. Any idea what is wrong with the second example? Old versions of gcc support something called "lvalue casts" -- if you cast something

Confusion about an error: lvalue required as unary '&' operand [duplicate]

百般思念 提交于 2019-12-01 06:03:53
This question already has an answer here: Not able to understand error condition wrt lvalues 3 answers I have been trying to understand pointer concepts by writing simple code, and I got an error problem, and it seems like I couldn't solve it or understand it. #include <stdio.h> int *foo(void); int main(void) { printf("%d\n", *foo()); return 0; } int *foo(void) { static int num = 1; ++num; return &(++num); } Here is the error message. error: lvalue required as unary ‘&’ operand return &(++num); Function 'foo()' returns a pointer to int, and main is supposed to be print the returned int by