0

Bear with me, I'm relatively new at C++.

Here is my project. I want to essentially create a simple game of chess.

I've got a base class gamePiece, which I'm thinking will eventually become an abstract class ( but right now it isn't)

The base class gamePiece has a bunch of data members in it: colorOfPiece, rankOfPiece, fileOfPiece, etc. It also has a function, void displayPieceInfo() which simply displays all of these values on the console via cout.

I am planning on having a number of derived classes from this one. Right now I have a "rook" subclass.

I want to add various types of pieces in a single vector, so I can later traverse it with an iterator.

Here is the problem I am running into.

vector<gamePiece> vectorOfAllGamePieces;
vector<gamePiece>::iterator itGamePieces; 

I push_back a Rook into the vector as the first element. All the constructors look like they are running fine, initializing the variables. Yet when I try to run the display function on the first element, the strings are empty/unitialized.

itGamePieces=vectorOfAllGamePieces.begin();
itGamePieces->displayPieceInfo();

If I were to push in a generic gamePiece into the vector instead, everything would display properly.

Am I allowed to even have a vector like this with mixed types of objects -- for example, an object of a derived class, an object of the parent class, an object of a second class derived from the same parent

And if so, why do you suppose the values of the data members aren't showing up properly, even after I have set them in the constructor?

1
  • 1
    The joys of slicing. You don't, in fact, have a vector of both base and derived. That vector only contains bases. Commented Feb 17, 2013 at 19:33

2 Answers 2

3

Not really, because of slicing. When you add a derived object to a vector<gamePiece>, it gets sliced and a gamePiece object is actually added to the vector.

You'll need to use a vector of (smart) pointers.

Sign up to request clarification or add additional context in comments.

Comments

1

The problem is like this, Rook is a GamePiece. But You Cannot Store a Rook in a place for GamePieces. You can only cast a Pointer to Rook to a Pointer to GamePiece. So your vector must be saving NOT GamePieces but GamePiece * (Pointers to GamePieces).

std::vector<GamePiece *> vectorOfAllGamePieces;

But this could give you trouble in memory management. Another way is to store Handles without using pointers. ( a handle is something like a pointer to a pointer ). Or of course you can use smart pointers.

A quick hack will be ( this is a "hack" which is "evil" in the eyes of some people ),

std::vector<Rook> allRooks;
std::vector<Pawns> allPawns;
.....
std::vector<Queen> allQueens;
King theKing;

std::vector<GamePiece *> allPieces;

The allPieces will contain pointers to objects in the other vectors. I think this is kind of OK when you know exactly all the types of pieces ( i guess this is chess ).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.