Thursday, July 9, 2009

Can someone explain how to create an overloaded assignment operator in C++?

I need an overloaded assignment operator, and a copy constructor. Can't seem to figure it out. The .h file is pasted below.


//Hand.h


//header file


#ifndef HAND_H


#define HAND_H


#include "Card.h"





class Hand


{


public:


Hand();





//overloaded assignement opereator


Hand%26amp; Hand::operator=(Hand%26amp; c);





virtual ~Hand();





//copy constructor


Hand(Hand%26amp; c);





//adds a card to the hand


void Add(Card* pCard);





//clears hand of all cards


void Clear();





//gets hand total value, intelligently treats aces as 1 or 11


int GetTotal() const;





protected:


vector%26lt;Card*%26gt; m_Cards;


};


#endif


//Card.h


//Header File





#ifndef CARD_H


#define CARD_H





class Card


{


public:


enum rank {ACE = 1, TWO, ..};


enum suit {CLUBS, DIAMONDS, ..};





friend ostream%26amp; operator%26lt;%26lt;(ostream%26amp; os, const Card%26amp; aCard);





Card(rank r = ACE, suit s = SPADES, bool ifu = true);





//returns the value of a card, 1 - 11


int GetValue() const;


//flips a card


void Flip();


private:


rank m_Rank;


suit m_Suit;


bool m_IsFaceUp;


}

Can someone explain how to create an overloaded assignment operator in C++?
The purpose of both those methods is to create an instance of the class that is identical in behavior to an existing instance.





If you don't declare them yourselves, the compiler will do it for you if required and if it's possible for it to do so. So unless you have something specific to do in the copy, one normally let the compiler take care of generating them. The compiler will produce a version which simply copy each of the variable from one instance to the other.





If you do require to perform something different (which is normally the case if any of your members are pointers, you then redefine them.





Hand%26amp; Hand::operator=(const Hand%26amp; c);


Hand(const Hand%26amp; c);





Here your only member is a vector of card*. So both method would consist of inserting each card from the existing instance vector, and inserting them in the new instance vector. BUT you have to decide if the second instance will insert the exact same card in it's vector, or an identical copy of it. If you insert the same cards, they can't be deleted or modified by any of those hand without affecting the other and this is a big problem, and unless you have a card manager that is responsible for that this solution is very dangerous. So you're better to insert identical copy of the cards in the second vector. Which will require a copy constructor and assignment operator on the card too.. Or at least a clone method, which will allow you to create a copy of each card found in the first hand, and insert them into the second hand vector.





This being said, you will need a copy constructor and assignment operator for the class hand. But that class is quite simple, and the compiler will normally be able to produce them for you, since it only contains plain values.


No comments:

Post a Comment