简体   繁体   English

运算符=重载

[英]Operator= overloading

So we are assigned a project in the class that I am taking right now, and I am stuck trying to figure out how to properly deep copy an array of Cards. 因此,我们在我现在正在上的课程中分配了一个项目,而我一直试图找出如何正确深层复制Card系列的方法。 Below I will post the entire .cpp and .h for the class I am working on, I am fairly certain that it's in the operator= function, but all I am really looking for is just some tips on how to fix it. 在下面,我将为正在处理的类发布整个.cpp和.h,我可以肯定它位于operator =函数中,但是我真正想要的只是关于如何修复它的一些技巧。 vector is not allowed to be use, nor are strings. 向量不允许使用,字符串也不允许使用。 This is a beginner class and so we do not know too much. 这是一门初学者,所以我们知道的并不太多。 The main error I'm receiving is a _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) Please and thanks in advance. 我收到的主要错误是_BLOCK_TYPE_IS_VALID(pHead-> nBlockUse),请提前感谢。

//.h
#pragma once
#include "stdafx.h"
#include "Card.h"



class Player
{

private:

// Needed for unit tests
// DO NOT REMOVE OR RENAME
char* m_name;           // Can change size of array if needed

Card* m_hand;       // Can change size of array if needed


int m_numCards;     // The number of cards the player is currently holding
int m_maxCards;     // The number of cards the player can store (the number of elements in Hand)
int m_score;

public:

Player(const char* _name = "Player", int _maxCards = 5);

Player(const Player& _cpy);

virtual ~Player();

Player& operator=(const Player& _assign);

const char* GetName() const { return m_name; }

bool GetCard(int, Card&) const;

int GetNumCards() const { return m_numCards; }

int GetMaxCards() const { return m_maxCards; }

int GetScore() const { return m_score; }

/*MUTATORS*/

void SetName(const char* _name);

void AddToScore(int addScore);

bool AddCard(Card);

bool Discard(int index, Card&);

void Clear();

virtual void Show() const;




// Needed for unit tests
// DO NOT REMOVE
friend class CTestManager;

};


//.cpp
#include "Player.h"

Player::Player(const char* _name, int _maxCards){
m_name = NULL;
SetName(_name);
m_hand = NULL;

m_hand = new Card[_maxCards];


m_maxCards = _maxCards;
m_numCards = 0;
m_score = 0;
}

Player::Player(const Player& _cpy)
{
    m_name = NULL;
    SetName(_cpy.m_name);
    m_hand = NULL;
    m_hand = new Card(*_cpy.m_hand);


m_maxCards = _cpy.m_maxCards;
m_numCards = _cpy.m_numCards;
m_score = _cpy.m_score;
}

Player::~Player(){
delete[] m_name;
delete[] m_hand;
}

Player& Player::operator=(const Player& _assign)
{
if (this != &_assign)
{

    delete[] m_name;
    SetName(_assign.m_name);

    delete[] m_hand;
            //enter implemented deep copy here//

    m_maxCards = _assign.m_maxCards;
    m_numCards = _assign.m_numCards;

    m_score = _assign.m_score;
}
return *this;
}

bool Player::GetCard(int index, Card& _Card) const{
if (index < m_numCards && index >= 0){
    _Card = m_hand[index];
    return true;
}
return false;
}

/*MUTATORS*/

void Player::SetName(const char* _name){
delete[] m_name;
int len = strlen(_name) + 1;
m_name = new char[len];
strcpy_s(m_name, len, _name);
}

void Player::AddToScore(int addScore){
m_score += addScore;

}

bool Player::AddCard(Card _addCard){
if (m_numCards != m_maxCards){
    m_hand[m_numCards++] = _addCard;
    return true;
}
return false;
}

bool Player::Discard(int discardIndex, Card& _discardCard){
if (discardIndex >= m_numCards){
    return false;
}
else{
    _discardCard = m_hand[discardIndex];
    for (int i = discardIndex; i < m_maxCards - 1; i++){
        m_hand[i] = m_hand[i + 1];
    }
    m_numCards--;
    return true;
}
}

void Player::Clear(){
m_numCards = 0;

}

void Player::Show() const{

}

If there is any tips that anyone can give me I would be greatly appreciative :D Thanks again. 如果有任何人可以给我的提示,我将非常感激:D再次感谢。

Use std::vector and be done with it. 使用std::vector并完成它。 It supports assignment. 它支持分配。 The question (as I'm writing this) does not indicate a prohibition against using std::vector . 这个问题(在我写这篇文章时)并不表示禁止使用std::vector

sounds more like a codereview kind of question but anyway here are some "tips": 听起来更像是codereview之类的问题,但无论如何,这里有一些“技巧”:

your copy constructor Player(const Player& _cpy) does not copy the array properly, you need to loop through the _cpy.m_hand and copy each element, what you are doing is basically just copying the first card and also making m_hand point to a single heap element and not an array. 您的复制构造函数Player(const Player& _cpy)不能正确复制数组,您需要遍历_cpy.m_hand并复制每个元素,您要做的基本上是复制第一张卡并将m_hand指向单个堆元素而不是数组。 this will cause an error when you later delete it since you are assuming m_hand being an array in the dtor (delete [] m_hand). 由于您假设m_hand是dtor中的数组(删除[] m_hand),因此在以后删除它时将导致错误。

Player& Player::operator=(const Player& _assign) here you need to first allocate an array to hold the copy, then do a for loop and copy each card to the new array. Player& Player::operator=(const Player& _assign)在这里,您需要首先分配一个数组来保存副本,然后进行for循环并将每张卡复制到新数组中。

eg 例如

m_hand = new Card[_assign.m_maxCards]; 
for (int i = 0; i < _assign.m_numCards; ++i)
{
  m_hand[i] = _assign.m_hand[i]; 
}
m_maxCards = _assign.m_maxCards;
...

Important: The way you copy cards depends on how you implement the Card class but you haven't provided a declaration of Card so I just assume it is a POD type. 重要说明:复制卡的方式取决于实现Card类的方式,但是您没有提供Card声明,因此我仅假设它是POD类型。 If it contains more complicated structures then you would need some method to clone its contents. 如果它包含更复杂的结构,则需要某种方法来克隆其内容。

as a side note: when you declare a class, put the public parts first in the class, the implementation details are normally better kept out of view and are often not (should not be) particularly interesting for the user of the class. 附带说明:当您声明一个类时,将公共部分放在该类的第一位,通常最好不要隐瞒实现细节,并且对于类的用户来说通常(不是应该)特别有趣。

if you must use these old C-style strings then it could be a good idea to create a small helper function which simulates strdup() 如果您必须使用这些旧的C风格的字符串,那么最好创建一个模拟strdup()的小型辅助函数。

char* newdup( const char* str )
{
  char* ret = NULL;
  if ( str != NULL )
  {
    ret = new char[strlen( str ) + 1];
    strcpy_s( ret, len, str );
  }
  return ret; 
}

You have to copy each member in the Player class , So implement a method in Card class by overloading = operator which does the deep copy in the card class. 您必须复制Player类中的每个成员,因此,通过重载=运算符在Card类中实现一个方法,该方法会在card类中进行深度复制。 Then you can just use = operator in the Player's method just by adding 然后,您只需在Player的方法中使用=运算符,只需添加

m_hand = _assign.m_hand; m_hand = _assign.m_hand;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM