Does it make sense to implement the copy-assignment operator in a class with all const data members?
Imagine I have a class used to repr开发者_运维问答esent some trivial numerical data, like a vector. I want this class to be immutable in terms of its members, but still support correct copy-assignment behaviour.
This is what the shell of this class might look like:
class Vector {
public:
Vector(float _x, float _y);
private:
const float x;
const float y;
};
I would like to be able to assign a new value to a vector member within (for example) a class. An on-screen entity with a position, perhaps, represented using a non-const member of type Vector
.
This 'entity' must be mutable itself (perhaps supporting movement), e.g. the internal Vector
member used to store its position must change. Rather than modifying this member directly, I would rather replace it with an entirely new Vector
instance, initialised with the modified values.
In a language such as C#, I would simply compute the new values for X and Y based on the current Vector
instance, and assign a new Vector object initialised with these values to the member, discarding the previous value and letting the GC do the rest.
// Inside some class (C#/Java type)...
Vector position;
...
// Example function:
void Move(float _dX, float _dY) {
this.position = new Vector(_dX + position.X, _dY + position.Y);
}
This is where my limited ability with C++ comes into play, because I am unsure of how to implement operator=
in such a way that instances of this immutable type contained in dynamic memory do not simply 'disappear' and cause a leak.
So, I suppose, what I am asking is this:
Vector& operator=(const Vector& _rhs);
...
// In implementation file...
Vector& Vector::operator=(const Vector& _rhs) {
// WHAT ON EARTH GOES IN HERE?!?!?
}
The Vector
case is something I invented to get my point across, I can think of many sorts of types that should be immutable at the member level.
Am I wasting my time attempting to model immutable types?
Is there a better solution?
When you type
Vector position;
in a class definition, you define an instance of a Vector
class that will stay here forever. If that one is immutable, you're screwed.
In C#/Java, there is no way to do this. Instead you define references.
If you want to do the same thing in C++, you need pointers or auto_pointers
Vector* position;
That way, you can change the instance to which position
is pointing.
You shouldn't define the assignment operator of a immutable class. If you really need to, maybe you should make Vector
mutable, and use const Vector
when you need an immutable Vector
. You will just need to add const
qualifiers to method that are allowed with a const Vector
.
eg:
class Vector {
private:
float x;
float y;
public:
// allowed for const Vector
// because of the const before the '{'
float norm() const {
return hypot(x,y);
}
Vector& operator=(const Vector& o) {
x=o.x;
y=o.y;
return *this;
}
};
What you're trying to do doesn't make sense for use with the operator=. Reason being is you need an instance of your Vector class in order to use operator=, but you've stated you do not want to allow changes to the internals.
Normally, your operator= would like something like:
Vector::operator=(const Vector &rhs)
{
x = rhs.x;
y = rhs.y;
}
* Note the compiler would automatically generate this for you and it would be safe as you have no pointers,etc.
This operator= overload won't work for your scenario, b/c the data members x and y will not reassign b/c you have them as const. This is where you would use a copy-constructor with an initialization list. If you really wanted to have operator=, you could use const_cast inside the overload, but most would consider this poor form.
Vector::Vector(const Vector ©)
:x(copy.x), y(copy.y)
{
}
* Note the compiler would also automatically generate this type of copy constructor for you.
Then your code would create the object with
Vector newObj(oldObj);
Having stated all this to answer your question, why not just let the members be non-const and declare the Vector const?
const Vector myVector(5,10);
You have a logical inconsistency in your requirements. On the one hand, all objects of class Vector must be immutable, but on the other hand you want to be able to change them. These two requirements are in direct conflict with each other.
A better design might be to make the classes such that the only way to change the value is to assign a new object of the class to it:
class Vector {
public:
Vector(float _x, float _y);
/* Vector(const Vector&) = default; */
/* Vector& operator=(const Vector&) = default; */
float x() const;
float y() const;
/* No non-const accessors!! */
private:
float x;
float y;
};
Once initialised, instances of this class can't be modified, except by completely overwriting them with a new instance.
If you're concerned about rule of three here, make the class noncopyable
, or declare the copy assignment operator as private
(and leave it undefined).
If that's not appropriate for your datatype, then that is an indication that those data members should not be const
.
I think you are misunderstanding const correctness somewhat. If you really want an immutable structure, having an operator= does not make sense.
However, I fail to see why the code below doesn't fit your needs:
struct Vector
{
Vector(float, float);
Vector(Vector const&);
private:
const float x, y;
// Disable automatic generation of operator=
Vector& operator=(Vector const&);
};
This should do it.
Vector& Vector::operator=(const Vector& _rhs) {
Vector tmp( _rhs.x, _rhs.y );
std::swap( *this, tmp );
return *this;
}
精彩评论