Overriding = operator in C++

17,593

Solution 1

It's been a while, but I think you want

Vec3& Vec3::operator=(const Point &a) 
{
    x = a.x; y = a.y; z = a.z;

    return *this;  // Return a reference to myself.
}

Assignment modifies 'this', so it can't be const. It doesn't return a new Vec3, it modifies an existing one. You will also probably want a copy constructor from Point, that does the same.

Solution 2

You want this:

Vec3 & Vec3::operator =(const Point &a) 
{
x = a.x;
y = a.y;
z = a.z;
return *this;
}
  1. Assignment should modify the this object, not return something
  2. Return a reference to object just modified

Solution 3

Assignment operator work like this.

Vec3.h:

Vec3& operator = (const Point &a);

Vec3.cpp:

Vec3& Vec3::operator = (const Point &a)
{
    x = a.x;
    y = a.y;
    z = a.z;
    return *this;
}

Notice that you're modifying this object and return a non-const reference to it.

Solution 4

I agree with sheepsimulator in the fact that copy assignment operator should have the same behavior than copy constructor has. According with HIGH·INTEGRITY C++ CODING STANDARD MANUAL, you should implement an explicit conversion operator:

class Point { explicit operator Vec3() { return Vec3(this->x,this->y,this->z);  } };

Solution 5

It's more common to do this with a conversion constructor:

Vec3(const Point& p) : x(p.x), y(p.y), z(p.z) {}

This will also allow the assignment that you want.

Share:
17,593

Related videos on Youtube

Aero
Author by

Aero

Updated on June 04, 2022

Comments

  • Aero
    Aero about 2 years

    I am trying to override the = operator so that I can change my Point class into a Vector3 class.

    Point tp = p2 - p1;
    Vec3 v;
    v = tp;
    

    The problem I am facing is that, "v" will have its x,y,z members equal to zero all the time.

    Vec3.h:

    Vec3 operator =(Point a) const;
    

    Vec3.cpp:

    Vec3 Vec3::operator =(Point a) const
        {
            return Vec3(a.x,a.y,a.z);
        }
    

    Thanks for all the help once again :)

    • Frédéric Hamidi
      Frédéric Hamidi over 13 years
      Technically, the difference between two points should be a vector, not a point (ideally, everything should be vectors, but I digress). Maybe you just wanted to implement Vec3 Point::operator -(Point other) in the first place?
  • James McNellis
    James McNellis over 13 years
    The conversion operator should be const-qualified (though, I'd be very wary of using a conversion operator; if you want an implicit conversion, a converting constructor is usually less error-prone).
  • Mike DeSimone
    Mike DeSimone over 13 years
    IIRC, there's a trinity here somewhere. It says that if you override operator=, you also need to put in a copy constructor and override one other thing... and I'm blanking on it.
  • GManNickG
    GManNickG over 13 years
    @Mike: That's only if the class manages resources. It's The Rule of Three. And you use the copy-and-swap idiom to implement it. It isn't needed here, though implementing this is surely an indicator of bad design.
  • Aero
    Aero over 13 years
    Yeah I am still new to this business :) Thanks for all the help

Related