在具有所有const数据成员的类中实现复制赋值运算符是否有意义? [英] Does it make sense to implement the copy-assignment operator in a class with all const data members?

查看:146
本文介绍了在具有所有const数据成员的类中实现复制赋值运算符是否有意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个类用于表示一些琐碎的数值数据,像一个向量。我希望这个类在其成员方面是不可变的,但仍然支持正确的复制赋值行为。



这是这个类的shell可能如下: / p>

  class Vector {
public:
Vector(float _x,float _y);

private:
const float x;
const float y;
};

我想能够为一个向量成员赋予一个新值类。具有位置的屏幕实体,可能使用类型 Vector 的非const成员表示。



这个实体本身必须是可变的(可能支持运动),例如用于存储其位置的内部向量成员必须更改。而不是直接修改这个成员,我宁愿用一个全新的 Vector 实例替换,用修改后的值初始化。



在C#等语言中,我只是根据当前的 Vector 计算X和Y的新值,并分配一个新的Vector对象

  //在一些类(C#/ Java)中,类型)... 
向量位置;
...
//示例函数:
void Move(float _dX,float _dY){
this.position = new Vector(_dX + position.X,_dY + position .Y);
}

这是我使用C ++的能力有限的地方,因为我不确定如何实现 operator = ,使得动态内存中包含的不可变类型的实例不会简单地消失并导致泄漏。



所以,我想,我要求的是这样:

  Vector& operator =(const Vector& _rhs); 
...
//在实现文件中...
Vector& Vector :: operator =(const Vector& _rhs){
//这里的地球是什么?
}

是我发明的东西,是为了得到我的观点,我可以想到在成员级别应该是不可变的许多类型。



我浪费我的时间试图模型不可变类型?



是否有更好的解决方案?

解决方案

您键入

 向量位置; 

在类定义中,您定义一个 Vector 类将永远留在这里。如果那个是不可变的,你会感到困惑。



在C#/ Java中,没有办法做到这一点。



<$>如果你想在C ++中做同样的事情,你需要指针或auto_pointers



<$> p $ p> Vector * position;

这样,您可以更改位置指向。



您不应该定义不可变类的赋值运算符。如果你真的需要,也许你应该使 Vector mutable,并使用 const Vector 当你需要一个不可变的 Vector 。你只需要添加 const Vector 允许的 const 限定符到方法。



例如:

  class Vector {
private:
float x ;
float y;
public:
//允许const向量
//因为在'{'
之前的const float norm()const {
return hypot(x, y)。
}

Vector& operator =(const Vector& o){
x = o.x;
y = o.y;
return * this;
}
};


Imagine I have a class used to represent 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;
    }
};

这篇关于在具有所有const数据成员的类中实现复制赋值运算符是否有意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆