26
loading...
This website collects cookies to deliver better user experience
int a = 10;
int a;
10 = a; // NOT VALID!
int a = 10;
int b = a; // VALID
int getValue() {
return 10;
}
int main() {
getValue() = 20; // ERROR
}
int& getValue() {
static int a = 10;
return a;
}
int main() {
getValue() = 20; // NO ERRORS.
}
void setValue(int& a) {
// Do something
}
void setValue (int&& a) {
// Do something
}
int& a = 10; // NOT VALID
const int& b = 10; // VALID
// accepts only lvalue
void showFullName_1(string& fullName) {
cout<<fullName<<endl;
}
// accepts both lvalue and rvalue
void showFullName_2(const string& fullName) {
cout << fullName <<endl;
}
// accepts only rvalue
void showFullName_3(string&& fullName) {
cout << fullName <<endl;
}
int main() {
string firstName = "john";
string lastName = "doe";
string fullName = firstName + lastName; //assigning to an lvalue
showFullName_1(fullName); //works
showFullName_1(firstName + lastName); // doesn't work because firstName + lastName is a rvalue
showFullName_2(fullName); // works
showFullName_2(firstName + lastName); // works
showFullName_3(fullName); // doesn't work
showFullName_3(firstName + lastName); // works
}
26