Consider the following proxy class:
class VertexProxy
{
public:
VertexProxy(double* x, double* y, double* z)
: x_(x), y_(y), z_(z) {}
VertexProxy(const VertexProxy& rhs)
: x_(rhs.x_), y_(rhs.y_), z_(rhs.z_) {}
// Coordinate getters
double x() const {return *x_;}
double y() const {return *y_;}
double z() const {return *z_;}
// Coordinate setters
VertexProxy& x(double val) {*x_ = val; return *this;}
VertexProxy& y(double val) {*y_ = val; return *this;}
VertexProxy& z(double val) {*z_ = val; return *this;}
VertexProxy& operator=(const VertexProxy& rhs)
{
// Should it be this
x_ = rhs.x_; y_ = rhs.y_; z_ = rhs.z_;
// or this?
*x_ = *rhs.x_; *y_ = *rhs.y_; *z_ = *rhs.z_;
return *this;
}
private:
double* x_; double* y_; double* z_;
};
I need to be able to reset the proxy so that it holds different coordinate pointers (similarly to boost::shared_ptr.reset(). In addition, I would like to be able to assign the coordinate values to the ones from a different proxy ( i.e. proxy1.assign(proxy2) ).
What should be the meaning of operator= in my class above? To copy rhs‘s pointers (shallow copy) or rhs‘s values? Or should I just make operator= private and provide two members functions to avoid the ambiguity of operator=?
EDIT:
Ok, here’s some background information. I’m writing a wrapper around a 3rd party GIS library (shapelib), which stores vertex coordinates (x,y,z,m) in separate arrays (instead of an array of structs). My proxy class is used to make this struct of arrays appear more like an array of structs. It works in tandem with a custom vertex iterator class that makes it much easier to work with ranges of vertices.
Shapelib handles the memory management. All my proxy class does is present a different “view” into the vertex data. When the user manipulates vertex coordinates using my proxy, it actually manipulates the vertex coordinates in the shapelib shape object.
It’s pretty simple. Do you want VertexProxy to act like a pointer, or a value? If you’d rather it acted like a pointer, then copy the pointers, if you’d rather it acted like a value, copy the values. Nobody can tell you that your class is a pointer or a value (especially since you seem to have something somewhat unusual). If you want better advice, we’d need to know what holds the actual doubles and why.
Quick edit:
Seems to me like actually, if you did the dereference, you’d have it acting like a reference or a pointer. However, the original point remains the same.