C++ << operator overloading without friend function
<p>Exactly as the topic says. Is there a possibility to do that? I was able to achieve this in overloading '+' operator, however, I could not do this with '&lt;&lt;' operator. </p>

<p>This is an example of code that works for me with friend function:</p>

<pre><code>class Punkt2D
{
    int x,y;

    public:
        Punkt2D(int wartoscX, int wartoscY) : x(wartoscX), y(wartoscY) {}
        friend ostream&amp; operator&lt;&lt;(ostream&amp; out, Punkt2D&amp; punkt);
};

ostream&amp; operator&lt;&lt;(ostream&amp; out, Punkt2D&amp; punkt)
{
    out &lt;&lt; "(" &lt;&lt; punkt.x &lt;&lt; ", " &lt;&lt; punkt.y &lt;&lt; ")" &lt;&lt; endl; 
    return out;
}

int main()
{
    Punkt2D p1(10,15);

    cout &lt;&lt; p1 &lt;&lt; endl;
    return 0;
}
</code></pre>

<p>I tried this code on '+' without befriending the function. Is it also possible for other operators? Maybe it is a silly question, however I am quite new to C++ and could not find any resource on the topic :(</p>

<pre><code>class Vector
{
    public:

    double dx, dy;
    Vector() {dx=0; dy=0;}
    Vector(double x, double y) 
    {
        cout &lt;&lt; "Podaj x " &lt;&lt; endl;
        cin &gt;&gt;x;
        cout &lt;&lt; "Podaj y " &lt;&lt; endl;
        cin &gt;&gt; y;
        dx = x; dy = y;

    }
    Vector operator+ (Vector v);
};


Vector Vector::operator+ (Vector v)
{
    Vector tmpVector;
    tmpVector.dx = dx +v.dx;
    tmpVector.dy = dy+ v.dy;
    return tmpVector;
}

int main()
{
    double d,e;

    Vector a(d,e);
    Vector b(d,e);
    Vector c;
    c = a +b;
    cout&lt;&lt;endl &lt;&lt; c.dy &lt;&lt; " " &lt;&lt; c.dx;
    return 0;
}
</code></pre>