0

For some reason my assignment operator produces bus error when I try and use it:

hand1 = hand2;


//overload assignment operator
Hand Hand::operator=(Hand other_hand)
{

    if(&other_hand != this){
        name = other_hand.name;
        cards = other_hand.cards;

    }

    return *this;   
}

The error occurs right after the return statement

Dante Hoyte
  • 333
  • 2
  • 15

1 Answers1

1

First of all assignment should have a signature that looks like this:

Hand & Hand::operator=(const Hand &other_hand)

You probably do not want to passing and returning a copy as was pointed out but also you want to allow operating chaining i.e.:

hand1 = hand2 = hand3 ....

This is a basic reference. Also copy and swap was mentioned, this previous thread does a perfect job of explaining it.

Community
  • 1
  • 1
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740