Thread: Operator Overloading - RHS object is modified by assignment operation

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    Yes, I think I understand it quite well now, and it's working good. Here's the whole class now.
    Code:
    #ifndef VEC3F_H_INCLUDED
    #define VEC3F_H_INCLUDED
    
    #include <iostream>
    #include <math.h>
    
    class Vec3f {
    	public:
    	        Vec3f();
            	Vec3f(float xi, float yi, float zi);
    	        ~Vec3f() {}
    	        float x,y,z;
    
    	        Vec3f operator=(const Vec3f& rhs);
    	        Vec3f operator+(const Vec3f& rhs);
    	        Vec3f operator-(const Vec3f& rhs);
    	        Vec3f operator*(const float& rhs);
    	        Vec3f operator/(const float& rhs);
    	        Vec3f operator+=(const Vec3f& rhs);
    	        Vec3f operator-=(const Vec3f& rhs);
    	        Vec3f operator*=(const float& rhs);
    	        Vec3f operator/=(const float& rhs);
    
    	        float getMag();
    	        void normalize();
    	        float dot(Vec3f rhs);
    	        Vec3f cross(Vec3f rhs);
    	        float getXAngle();
    	        float getYAngle();
    	        void setAngles(float xa, float ya);
    
    	        friend std::ostream& operator<< (std::ostream& stream, const Vec3f& v);
    	protected:
    	        float xAngle_;
    	        float yAngle_;
    };
    
    #endif //VEC3F_H_INCLUDED
    Code:
    #include "vec3f.h"
    
    Vec3f::Vec3f()
    {
        x = y = z = 0.0;
    }
    
    Vec3f::Vec3f(float xi, float yi, float zi)
    {
        x = xi;
        y = yi;
        z = zi;
    }
    
    Vec3f Vec3f::operator=(const Vec3f& rhs)
    {
        x = rhs.x;
        y = rhs.y;
        z = rhs.z;
        return *this;
    }
    
    Vec3f Vec3f::operator+(const Vec3f& rhs)
    {
        return Vec3f(x + rhs.x, y + rhs.y, z + rhs.z);
    }
    
    Vec3f Vec3f::operator-(const Vec3f& rhs)
    {
        return Vec3f(x - rhs.x, y - rhs.y, z - rhs.z);
    }
    
    Vec3f Vec3f::operator*(const float& rhs)
    {
        return Vec3f(x * rhs, y * rhs, z * rhs);
    }
    
    Vec3f Vec3f::operator/(const float& rhs)
    {
        return Vec3f(x / rhs, y / rhs, z / rhs);
    }
    
    Vec3f Vec3f::operator+=(const Vec3f& rhs)
    {
        x += rhs.x;
        y += rhs.y;
        z += rhs.z;
        return *this;
    }
    
    Vec3f Vec3f::operator-=(const Vec3f& rhs)
    {
        x -= rhs.x;
        y -= rhs.y;
        z -= rhs.z;
        return *this;
    }
    
    Vec3f Vec3f::operator*=(const float& rhs)
    {
        x *= rhs;
        y *= rhs;
        z *= rhs;
        return *this;
    }
    
    Vec3f Vec3f::operator/=(const float& rhs)
    {
        x /= rhs;
        y /= rhs;
        z /= rhs;
        return *this;
    }
    
    float Vec3f::getMag()
    {
        return sqrtf(x*x + y*y + z*z);
    }
    
    
    void Vec3f::normalize()
    {
        *this /= this->getMag();
    }
    
    float Vec3f::dot(Vec3f rhs)
    {
        float r = 0.0;
        r = x * rhs.x;
        r += y * rhs.y;
        r += z * rhs.z;
        return r;
    }
    
    Vec3f Vec3f::cross(Vec3f rhs)
    {
        Vec3f r;
        r.x =  (this->y * rhs.z) - (this->z * rhs.y);
        r.y =-((this->x * rhs.z) - (this->z * rhs.x));
        r.z =  (this->x * rhs.y) - (this->y * rhs.x);
        return r;
    }
    
    float Vec3f::getXAngle()
    {
        /*code here to calculate the angle*/
        return xAngle_;
    }
    
    float Vec3f::getYAngle()
    {
        /*code here to calculate the angle*/
        return yAngle_;
    }
    
    void Vec3f::setAngles(float xa, float ya)
    {
        /*See notes about this function*/
        xAngle_ = xa;
        yAngle_ = ya;
        x = -sinf(xa);
        y = tanf(ya);
        z = -cosf(xa);
        this->normalize();
    }
    
    std::ostream& operator<<(std::ostream& stream, const Vec3f& v)
    {
        std::cout << "x = " << v.x << ", y = " << v.y << ", z = " << v.z;
        return stream;
    }
    Let me know if you have any suggestions.
    Last edited by IdioticCreation; 12-27-2010 at 02:18 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 60
    Last Post: 12-20-2005, 11:36 PM
  2. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  3. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM
  4. Linked List Templates and Object Types
    By ventolin in forum C++ Programming
    Replies: 10
    Last Post: 06-16-2004, 12:05 PM
  5. How would you do this (object interlinking)
    By darksaidin in forum C++ Programming
    Replies: 7
    Last Post: 08-30-2003, 12:08 AM