22

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. 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. 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. I use a vector: vector allPlayers; The members of the class Player are:

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. Any help?

Community
  • 1
  • 1
arjacsoh
  • 8,932
  • 28
  • 106
  • 166

3 Answers3

25

A C++ 'reference' can only be initialized, not assigned:

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.

xtofl
  • 40,723
  • 12
  • 105
  • 192
10

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.

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. You might want to use shared_ptr for the values.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • 1
    `std::reference_wrapper` would be better if the referred object is not nullable and/or owned. That has the correct assignment semantics (rebinding) also. – underscore_d Jun 20 '20 at 15:14
-1

It is really a hack over c++ design, but you can use placement new on 'this' to do that. I.e.

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

MyClass& MyClass::operator=(const MyClass& other)
{
    new (this) MyClass(other.myReference);
    return *this;
}
Andrew
  • 101
  • 1
  • 8
  • 1
    I know why this answer received 3 thumb-downs. The placement new creates potential memory leak if (**and only if**) the class has non-trivial-non-reference members. However, I still think it to be a useful trick provided the "only-if" condition doesn't exist. – Zhou Apr 29 '21 at 11:02
  • 1
    downvotes are fair here and I wouldn't recommend it to anyone by myself but yeah, it required an explanation I didn't provide. I had only an intension to give a possible variant that exactly answers the question without estimation how bad it is, votes are for that purpose. you are right about potential memory leaks in some cases – Andrew Apr 30 '21 at 12:21