简体   繁体   English

具有引用类成员的赋值运算符

[英]Assignment operator with reference class member

As long as new issues are growing out of my previous question Overloaded assignment operator causes warning about recursion , I was legitimately urged to post this as new one.只要我之前的问题出现了新问题Overloaded assignment operator 导致关于递归的警告,我就被合理地敦促将其作为新问题发布。 I have a reference class member in my class Player and I want to implement the copy constructor and the assignment operator (=) of this class.我的类 Player 中有一个引用类成员,我想实现这个类的复制构造函数和赋值运算符 (=)。 I have to mention that the purpose is the fine working of the function vector.erase because without that it does not work properly as far as I am concerned.我不得不提到,目的是函数 vector.erase 的正常工作,因为没有它,就我而言它无法正常工作。 I use a vector: vector allPlayers;我使用一个向量:vector allPlayers; The members of the class Player are: Player 类的成员是:

class Player
{

  private:
  int ID;
  int pMoney;
  int doubleIndicator;
  int squarePosition;
  Bank& bank;
  string pName;
  Square* capturedSquare;
  multimap<string, PropertySquare*> squaresColBought;
  multimap<string, House*> housesColBuilt;

}

Is it mandatory to avoid the use of reference as class member if I want to implement the assignment operator?如果我想实现赋值运算符,是否必须避免使用引用作为类成员? What about the map members?地图成员呢? How should I finally implement the assignment operator?我最终应该如何实现赋值运算符?

Another issue of utmost importance of which I am unaware is what happens to the objects pointed by pointers class members when I erase the iterator of the vector which hold the Player.另一个我不知道的最重要的问题是,当我擦除持有 Player 的向量的迭代器时,指针类成员指向的对象会发生什么。 Any help?有什么帮助吗?

A C++ 'reference' can only be initialized, not assigned: C++“引用”只能被初始化,不能被赋值:

int value1(1), value2(2);
int& ref1 = value1; // OK
int& ref2; // compile error: reference not initialized
int& ref3=ref1; // OK: ref3 refers to the same variable as ref1
ref1=value2; // equivalent to 'value1=value2'.

Therefor, an object containing a reference can only be initialized, too!因此,包含引用的对象也只能初始化!

So indeed: if you need assignment on a class, that class cannot have reference member variables.确实如此:如果您需要对类进行赋值,则该类不能具有引用成员变量。 (as a matter of fact, it could, but the assignment cannot make these members refer to another location) (事实上​​是可以的,但是赋值不能让这些成员指向另一个位置)

When you think about this, it makes sense:当你想到这一点时,它是有道理的:

The reference concept defines 'an alias' for another variable.引用概念为另一个变量定义了“别名”。 The aliasing implies that anything you do to your reference, you actually do to the referenced location.别名意味着您对引用所做的任何事情,实际上都是对引用的位置所做的。 When you apply assignment to this alias, actually you assign to the referenced location.当您对这个别名应用分配时,实际上您分配给了引用的位置。 The purpose of the reference would be lost if you were able to make it point to a different location using assignment.如果您能够使用赋值将其指向不同的位置,则引用的目的将丢失。

If the latter is what you need, you should use a pointer.如果后者是你所需要的,你应该使用指针。

I would refrain from using a reference member when you want an assignment operator.当您需要赋值运算符时,我会避免使用引用成员。 If you use a (smart) pointer instead, you can just do如果您改用(智能)指针,则可以这样做

Player &operator=(Player const &other)
{
    bankPtr = other.bankPtr;
    // copy other members
}

In the current situation, bank = other.bank will copy the contents of other.bank instead of pointing this->bank to the contents referenced by other.bank .在当前形势下, bank = other.bank将复制的内容other.bank而不是指向this->bank被引用的内容other.bank

As for the multimap -typed members, they can be copied without problems, but do keep in mind that you'll get a "deep" copy of the keys (since they're of type string ) but a "shallow" pointer copy of the values, so you end up with shared state.至于multimap类型的成员,可以毫无问题地复制它们,但请记住,您将获得键的“深”副本(因为它们是string类型),但是“浅”指针副本值,因此您最终会获得共享状态。 You might want to use shared_ptr for the values.您可能希望对值使用shared_ptr

It is really a hack over c++ design, but you can use placement new on 'this' to do that.这确实是对 C++ 设计的一种黑客攻击,但您可以在“this”上使用placement new 来做到这一点。 Ie IE

MyClass::MyClass(ReferenceType& referenceTarget):
    myReference(referenceTarget)
{}

MyClass& MyClass::operator=(const MyClass& other)
{
    new (this) MyClass(other.myReference);
    return *this;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类成员引用另一个:赋值运算符崩溃 - class member as reference to another: crash in assignment operator 为具有引用成员变量的类创建一个赋值运算符 - Creating an Assignment Operator for a class that has Reference Member Variables 具有常数 class 成员的赋值运算符 - Assignment operator with a constant class member 警告分配运算符类参考 - warning assignment operator class reference 如果类具有引用成员,为什么将综合的复制分配运算符定义为已删除? - Why synthesized copy-assignment operator is defined as deleted if the class has a reference member? 如果类具有引用数据成员,为什么默认赋值运算符不由编译器合成 - Why a default assignment operator not synthesis by compiler if a class has a reference data member 当类成员是引用时,无法生成默认赋值运算符?(在C ++中) - Cannot generate default assignment operator when a class member is a reference?(in C++) 如何重载类成员变量上的赋值运算符 - how to overload assignment operator on class member variable 内部类中的默认赋值运算符,带有引用成员 - Default assignment operator in inner class with reference members 清除具有删除了赋值运算符的成员的类实例 - Clearing a class instance with a member that has a deleted assignment operator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM