¡Esta es una revisión vieja del documento!
Main C++ expression types are rvalue and lvalue. It depends on which part of an assignment they appear (right-hand: rvalue, left-hand: lvalue).
Mainly, an lvalue refers to an object that persists beyond a single expression (all variables, including nonmodifiable (const) variables, are lvalues), they are also called locator value because represents an object that occupies some identifiable location in memory. An rvalue is a temporary value that does not persist beyond the expression that uses it.
In the next example, x is an lvalue because it persists beyond the expression that defines it. The expression 3 + 4 is an rvalue because it evaluates to a temporary value that does not persist beyond the expression that defines it.
#include <iostream> using namespace std; int main() { int x = 3 + 4; cout << x << endl; }
// Incorrect usage: The left operand must be an lvalue (C2106). 7 = i; // C2106 j * 4 = 7; // C2106 // Correct usage: the dereferenced pointer is an lvalue. *p = i; const int ci = 7; // Incorrect usage: the variable is a non-modifiable lvalue (C3892). ci = 9; // C3892 // Correct usage: the conditional operator returns an lvalue. ((i < 3) ? i : j) = 7;
An xvalue (an “eXpiring” value) also refers to an object, is the result of certain kinds of expressions involving rvalue references.
Since C++11 the rvalue is named prvalue (pure rvalue), and is redefined a an rvalue that is not an xvalue.
There also are the glvalue type expression (generalized lvalue) which are an lvalue or an xvalue.
const over #define: const is typed, #define macros are not. const is scoped by C block, #define applies to a file.
const is most useful with parameter passing. If you see const used on a prototype with poiinters, you know it is safe to pass your array or struct because the function will not alter it. No const and it can.
void myfunc(const char x);
This means that the parameter x is a char whose value cannot be changed inside the function. For example:
void myfunc(const char x) { char y = x; // OK x = y; // failure - x is `const` }
const member functions prevent modification of any class member.
int myclass::myfunc() const { // do stuff that leaves members unchanged }
If you have specific class members that need to be modifiable in const member functions, you can declare them mutable.
const char* p is a pointer to a const char. char const* p is a pointer to a char const. However, consider: char * const p is a const pointer to a (non-const) char. I.e. you can change the actual char, but not the pointer pointing to it.
int main { const int i = 10; const int j = i+10; // Works fine i++; // This leads to Compile time error }
This means that the pointer is pointing to a const variable: const int* u;
To make the pointer const, we have to put the const keyword to the right of the *.
int x = 1; int* const w = &x;
Here, w is a pointer, which is const, that points to an int. Now we can't change the pointer but can change the value that it points to.
We can also have a const pointer pointing to a const variable: const int* const x;
class Test { const int i; public: Test (int x) { i=x; } }; int main() { Test t(10); Test s(20); }
In this program, i is a const data member, in every object its independent copy is present, hence it is initialized with each object using constructor. Once initialized, it cannot be changed.
When an object is declared or created with const, its data members can never be changed, during object's lifetime.
const class_name object;
Always use const for function parameters passed by reference where the function does not modify (or free) the data pointed to: int find(const int *data, size_t size, int value);
Never use const in a function prototype for a parameter passed by value. It has no meaning and is hence just 'noise'.
Where appropriate, use const volatile on locations that cannot be changed by the program but might still change. Hardware registers are the typical use case here, for example a status register that reflects a device state.
+-------+
| +-+ |
| ^ | |
char *str[10];
^ ^ | |
| +---+ |
+-----------+
What is str?