Why would:
    vectorClass nv = vectors[0] + vectors[1];
    cout %26lt;%26lt;  vectors[0] %26lt;%26lt; " + " %26lt;%26lt; vectors[1] %26lt;%26lt; " = " %26lt;%26lt; nv %26lt;%26lt;  endl;
 
work,  While:
    cout %26lt;%26lt;  vectors[0] %26lt;%26lt; " + " %26lt;%26lt; vectors[1] %26lt;%26lt; " = " %26lt;%26lt; vectors[0] + vectors[1] %26lt;%26lt;  endl;
would not.
Here is some implementation:
vectorClass operator + ( const vectorClass %26amp;left, const vectorClass %26amp;right )
{
    int a, b, c;
    a = left.x + right.x;
    b = left.y + right.y;
    c = left.z + right.z;
    return vectorClass( a, b, c );
}
ostream %26amp; operator %26lt;%26lt; ( ostream %26amp;out, vectorClass %26amp;right )
{
    out %26lt;%26lt; "[" %26lt;%26lt; right.x %26lt;%26lt; ", " %26lt;%26lt; right.y %26lt;%26lt; ", " %26lt;%26lt; right.z %26lt;%26lt; "]";
    return out;
}
C++ operator overloading question?
add another overloaded function like this
vectorClass operator + (const vectorClass %26amp;right )
{
int a, b, c;
a = x + right.x;
b = y + right.y;
c = z + right.z;
return vectorClass( a, b, c );
}
Reply:Could it be the simple allocation of the class?
Not a ++ major, but even my old school, we had to allocate the memory....which is what vectorClass nv = 
does.
Subscribe to:
Post Comments (Atom)
 
No comments:
Post a Comment