A static function cannot access non-static variables
static method cannot access non-static variables
A static function is not tied to an instance (there is no object); it is like a function written outside the class
static void Print(classname e){
std::cout<<e.x << std::endl;
}
'''
>Compile error when passing a temporary as a non-const reference argument - mlove Programming (Programmer Baobao)
Tags: QT qt string linux
1. Cause of the error and how to fix it
When running on Linux and compiling with g++, the code below produces the error: invalid initialization of non-const reference of type ‘std::string&’ from a temporary of type ‘std::string’.
In other words, a non-const reference cannot be initialized from a temporary. That is, the error occurs during argument passing. The offending code is as follows
void print(string& str)
{
cout<<str<<endl;
}
//Calling it like this triggers the error described above
print("hello world");
The cause of the error is that the compiler constructs a temporary string object from the string ”hello world”, and this temporary object is const. When the temporary is passed to a non-const string& reference, the error occurs because binding a non-const reference requires the object to be non-const as well, while here the temporary string object is a const object. The fix, therefore, is to change the parameter of print() to a const reference. With the code modified as follows, it compiles successfully.
void print(const string& str)
{
cout<<str<<endl;
}
//Compiles successfully
print("hello world");
This code also illustrates a C++ programming principle advocated in Effective C++: use const whenever possible. Doing so makes code more robust and exposes errors at compile time.
2. Are all temporary objects const?
Why must a temporary object be passed as a const reference when used as a reference argument? Many people explain that temporaries are constants that cannot be assigned or modified, so the compiler reports an error when they are passed as non-const references. This explanation works for understanding why temporaries cannot be non-const reference arguments, but it is not precise. Some even go further and claim that all temporary objects are const. So we should approach opinions and resources found online with caution and healthy scepticism, following the "take what is useful" approach advocated by Lu Xun: refer to them and learn from them critically.
In fact, a temporary can be used as an lvalue (LValue) and assigned to; look at the following code:
class IntClass{
private:
int x;
public:
IntClass(int value):x(value){
}
friend ostream& operator<<( ostream &os, const IntClass &intc);
};
//Overload the output operator<<
ostream& operator<<( ostream &os, const IntClass &intc)
{
os<<intc.x;
return os;
}
//Print function
void print(IntClass & intc)
{
cout<<intc<<endl;
//Modify this temporary object through the reference
intc=8;
cout<<intc<<endl;
}
int main(int argc,char* argv[])
{
print(IntClass(6));
}
Program output:
6
8
The code above compiles and runs correctly without errors. IntClass(6) creates an unnamed temporary object, which is passed to a non-const reference, and the print function modifies this temporary through the reference. This shows that not all temporary objects are const.
So which temporaries are const and which are not? Here is a quote found online: "Temporaries of built-in types are const, whereas temporaries of user-defined types are not." I think this explains why the temporary in question can be used as an lvalue.
int* const a = new int;
Here the pointer a itself cannot be modified, e.g. a = &b; is an error
const int* a = new int; int const* a = new int; These two are the same; look at where const is placed
Here the content pointed to by a cannot be modified, e.g. *a = 2; is an error