Jump to content
  • Advertisement
Sign in to follow this  
oddeven21

Copy Ctor and assignment operator C++

This topic is 3166 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everyone, my first post. I am having some difficulty with correctly writing the Copy Constructor and overloading the assignment operator for some of my classes. Here are the classes and their relevant data members.
class Card
{
  //default copy ctor and assignemnt
  //operator are all thats required
  protected:
    //bunch of ints
};


class CardContainer
{
  //defauly copy ctor and 
  //assignment operator need 
  //to be overloaded
  protected:
    std::deque<Card> cards;
};      


class Cascade: public CardContainer
{
  //inherits its only data members
  //from CardContainer
};


class Board
{
  //default copy ctor and assignment
  //operator need to be overloaded
  private:
    std::vector<CardContainer *> containers;
    //initialised like this: containers.push_back(new Cascade));
};


int main
{
  Board *board = new Board;
  Board *board_backup = new Board;

  //later on I want board_backup to have a copy  
  //of what board points to
  board_backup = board;

  //if later on I need to restore board to its original
  //values then
  delete board;
  board = new Board(board_backup);

};


At certain points in the game, a copy of 'board' is given to 'board_backup'. I have written functions CopyContainers() and CopyCards(), both of which work. I have tried writing the copy ctor and assignment operators for class Board and class CardContainer but they do not work. I can post those attempts if you would like. I would really appreciate any help with this, thanks for your time.

Share this post


Link to post
Share on other sites
Advertisement
Is this homework by any chance? (Just asking so that we can tailor our answers appropriately.)

Meanwhile, I think the first question you'll want to ask yourself is: why are you using dynamic allocation? (Based on what you've posted so far, it seems that the use of dynamic allocation is the only reason you'd need to implement copy or assignment operators.)

Also, you have a memory leak in your 'main' function.

[Edit: I realize your 'main' function is just a sketch, more or less, but it still looks like you're missing some basic concepts with respect to pointers (as Bregma's post indicates).]

Share this post


Link to post
Share on other sites
I understand that

board = board_backup;

only makes board point the memory that board_backup points too. It does not give board its own copy of what board_backup points too. I guess my question would be then, can you overload assignment operators and copy constructors to work with pointers so that

board = board_backup

results in board containing its own copy of board_backup's memory

[EDIT]
First off, this is not homework. I am working on a game and I am also trying to learn more about copying dynamic memory within classes.

jyk: dynamic memory allocation is only taking place in class Board. This is why I want to overload the assignment operator and copy ctor for that class.

Will doing this allow me to copy what one board object points too?
Is it necesary to do the same for class CardContainer?

Also, disregard the main function. I wrote that for simplicities sake, and to help describe my question

Share this post


Link to post
Share on other sites
Quote:
Original post by oddeven21
I guess my question would be then, can you overload assignment operators and copy constructors to work with pointers so that

board = board_backup

results in board containing its own copy of board_backup's memory

You cannot overload operators for fundamental types (and pointer to ... is a fundamental type).

What you want is either use value semantics, for which copy constructors and assignment operator apply, or create a clone member function.
Board* clone(Board*);

Share this post


Link to post
Share on other sites
Quote:
Original post by oddeven21
Ok great thank you, that makes sense. I guess for now, I will stick to the Copy functions I currently have working
Actually all you need for copying that structure is code like this:
  Board *board = new Board;
Board *board_backup = new Board;

//Later on make a copy of what board points to
*board_backup = *board; // TADA!


This will call your assignment operator, or the compiler generated one if you dont provide one.

Share this post


Link to post
Share on other sites
Why do you have any pointers in the first place?


int main()
{
Board board, board_backup;

//later on I want board_backup to have a copy
//of what board points to
board_backup = board;

//if later on I need to restore board to its original
//values then
board = board_backup;
};

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!