hi guys , I have strange question , that why the pointer function member is l-value like
#include <iostream>
using namespace std;
class time{
private: int *t;
public: time():t(new int(10)){;}
int *func(){return t;}
int &funcRef(){return *t;}};
void main()
{
time time1;
int x=100;
time1.func()=&x;
time1.funcRef()=x; //funcRef() error : "error C2106: '=' : left operand must be l-value"
}
?
NOTE: I'M REALLY SORRY , BECAUSE I'M FROM JORDAN AND I'M NOT POWERFUL IN ENGLISH LANGUAGE
class time{
private: int *t;
public: time():t(newint(10)){;}
int *func(){return t;}
int &funcRef(){return *t;}};
void main()
{
time time1;
int x=100;
time1.func()=&x;
time1.funcRef()=x; //funcRef() error : "error C2106: '=' : left operand must be l-value"
}
I would say recheck your code and repost the question - the line you indicate is NOT an error.
You will need to read up on Rvalues and Lvalues (which will not be easy reading
as the standard does not really totally black and white answers.).
But essentially Lvalues are objects that are modifiable and can therefore
be the destination of an assignment operation).
Objects rturned by copy from a function (such as the int pointer
returned from yourint *func(){return t;} function are called
temporaries ( also known as Rvalues)
Rvalues are NOT Lvalues and therefore cannot be assigned to.
So your function call time1.func()=&x will give the error