C++ friend got no this?

Viewed 87

I just though about this and found no answer in the web. If a method isn't defined as static then it has a const pointer to a member of the class (this), But why this isn't true about friends?

For example the following won't compile:

class B {
private:
    int n;
public:
    B (int x) : n(x){}
    B operator+ (B &b)
    {
        return B(n+b.n);
    }
    friend ostream& operator<< (ostream &out, const B& b)
    {
        out << "B: " << this.n;//Bug is here
        return out;
    }
    bool operator <(const B & rhs)
    {
        return n<rhs.n;
    }
};
0 Answers
Related