vendredi 6 mars 2015

operator overload. q = q1+q2, q1 being modified but I want q1 and q2 to be intact


fist of all, sorry if I make grammar mistakes, english is not my native language.


so I saw operator overloading like 1 or 2 weeks ago and I think I know how it wokrs but I'm trying to overload the operator + so it returns a new CCuaternion which is working well but



CCuaternion(float w, float x, float y, float z) {
this->w = w;
this->x = x;
this->y = y;
this->z = z;
} //Ccuaternion constructor at Ccuaternion.h

CCuaternion operator + (CCuaternion &q2); // at CCuaternion.h

CCuaternion CCuaternion::operator + (CCuaternion &q2){ // at Ccuaternion.cpp
CCuaternion suma;

suma.setW(w += q2.getW());
suma.setX(x += q2.getX());
suma.setY(y += q2.getY());
suma.setZ(z += q2.getZ());

return suma; }


in my main I have an object CCuaternion called qsuma which recieve the CCuaternion that the overload is returnin it looks like this



q1 = CCuaternion(0, 1, 0, 1); // at main.cpp
q2 = CCuaternion(1, 0, 0, 1);

qsuma = (q1+q2);


qsuma ends up with the values 1, 1, 0, 2. as supposed to. q2 stays the same but q1 ends up with the same value as qsuma.




Aucun commentaire:

Enregistrer un commentaire