Reputation: 723
I have a class A,
class A{
private:
int num;
public:
A(int n){ num = n; };
int getNum(){
return num;
}
A operator+(const A &other){
int newNum = num + other.getNum();
return A(newNum);
};
};
Why does other.getNum()
give an error? I can access variables in other (other.num
) perfectly fine, but it seems that I can't ever use any of other's functions.
The error I get is something along the lines of
Invalid arguments: Candidates are int getNum().
I can write int test = getNum()
but not int test = other.getNum()
, but I'm almost sure I'm able to call other.getNum()
somehow.
Am I overlooking something?
Upvotes: 0
Views: 61
Reputation:
Other is marked const. Therefore, only const methods can be invoked on it. Either make other non-const or make getNum
a const method. In this case, making getNum
const would be the way to go.
The reason you can call getNum
on this
is because this is not const. Making a method const effectively makes the this pointer const.
Upvotes: 5