Page 1 of 1

Polymorphism and Arrays

Posted: Mon Jan 24, 2011 9:35 pm
by Maevik
I'm trying to create a class Deck (IE a deck of cards) that works with arrays of class Card. The part I'm having trouble with is that I want this deck to work with all kinds of cards and am therefore working with classes that inherit the class Card (In this case a class ResourceCard : public Card.) What I would like to be able to do is initialize an instance of Deck with an array of ResourceCard. Since they will have different memory sizes, I'm not sure polymorphism/casting can help me.

Code: Select all

int main()
{
	ResourceCard resourceCard;
	ResourceCard resourceCards[5];

       //enum Resource { NONE, WATER, FOREST, HILL, MOUNTAIN , ANY };

	resourceCard.setType( ANY );
	resourceCards[0] = resourceCard;
	resourceCard.setType( WATER );
	resourceCards[1] = resourceCard;
	resourceCard.setType( FOREST );
	resourceCards[2] = resourceCard;
	resourceCard.setType( HILL );
	resourceCards[3] = resourceCard;
	resourceCard.setType( MOUNTAIN );
	resourceCards[4] = resourceCard;


	Deck deck( resourceCards , 5 );     


	cin.get();
	return 0;
}

Code: Select all

//The constructor I'm using
Deck::Deck( Card p_newCards[] , int p_count )
{
	cardsInDeck = new Card[DECK_SIZE_LIMIT];
	count = 0;
	discardedCards = new Card[DECK_SIZE_LIMIT];
	discardCount = 0;
	lastDrawnCard = NULL;


	if( p_count <= DECK_SIZE_LIMIT && p_count > 0)
	{
		count = p_count;
		for( int i = 0 ; i < count ; i++ )
		{
			cardsInDeck[i] = p_newCards[i];          //This line causes the program crash on it's second iteration
		}
	}
}
So I realize I'm accessing memory I shouldn't since I'm sending an array of ResourceCard which takes up more memory than regular old Card. My question is, how do I fix this? It seems to me there should be a way to do this (or at least something like this.)

Re: Polymorphism and Arrays

Posted: Tue Jan 25, 2011 5:20 am
by adikid89
use a vector of dynamically allocated Cards? vector<Card*> cards; Since they're pointers polymorphism will work.

Re: Polymorphism and Arrays

Posted: Tue Jan 25, 2011 12:45 pm
by thejahooli
adikid89 wrote:use a vector of dynamically allocated Cards? vector<Card*> cards; Since they're pointers polymorphism will work.
You could just use an array of pointers to ResourceCards, a vector is not necessarily needed. Then you could just do this.

Code: Select all

ResourceCard *resourceCards[5];

resourceCards[0] = new ResourceCard();
resourceCrads[0]->SetType(/*Insert Type*/);
// And so on for all the cards

Re: Polymorphism and Arrays

Posted: Tue Jan 25, 2011 2:26 pm
by D-e-X
thejahooli wrote:
adikid89 wrote:use a vector of dynamically allocated Cards? vector<Card*> cards; Since they're pointers polymorphism will work.
You could just use an array of pointers to ResourceCards, a vector is not necessarily needed. Then you could just do this.

Code: Select all

ResourceCard *resourceCards[5];

resourceCards[0] = new ResourceCard();
resourceCrads[0]->SetType(/*Insert Type*/);
// And so on for all the cards

Code: Select all

const int NUM_RESOURCE_CARDS = 5;
ResourceCard* resourceCards[NUM_RESOURCE_CARDS];

for(int i = 0; i < NUM_RESOURCE_CARDS; ++i)
{
     resourceCards[i] = new ResourceCard;
     // codez . . .
}
//moar codez . . .
Because I can ;)

Re: Polymorphism and Arrays

Posted: Tue Jan 25, 2011 3:02 pm
by thejahooli
D-e-X wrote:

Code: Select all

const int NUM_RESOURCE_CARDS = 5;
ResourceCard* resourceCards[NUM_RESOURCE_CARDS];

for(int i = 0; i < NUM_RESOURCE_CARDS; ++i)
{
     resourceCards[i] = new ResourceCard;
     // codez . . .
}
//moar codez . . .
Because I can ;)
But then they would have to be the same type, whereas in his example he uses five different types for his cards.

Re: Polymorphism and Arrays

Posted: Tue Jan 25, 2011 3:51 pm
by GroundUpEngine
Your idea works fine (doesn't crash), unless i got the wrong idea here?

Code: Select all

#include <iostream>
using namespace std;

class Card
{
public:
};
enum Resource { NONE, WATER, FOREST, HILL, MOUNTAIN , ANY };
class ResourceCard : public Card
{
public:
    Resource res;
    void setType(Resource r)
    {
        res = r;
    }
};
#define DECK_SIZE_LIMIT 50
class Deck
{
public:
    Card *cardsInDeck, *discardedCards, *lastDrawnCard;
    int count, discardCount;

//The constructor I'm using
    Deck( Card p_newCards[] , int p_count )
    {
        cardsInDeck = new Card[DECK_SIZE_LIMIT];
        count = 0;
        discardedCards = new Card[DECK_SIZE_LIMIT];
        discardCount = 0;
        lastDrawnCard = NULL;


        if( p_count <= DECK_SIZE_LIMIT && p_count > 0)
        {
            count = p_count;
            for( int i = 0 ; i < count ; i++ )
            {
                cout << "i:" << count << endl;

                cardsInDeck[i] = p_newCards[i];
            }
        }
    }
};

int main()
{
    ResourceCard resourceCard;
    ResourceCard resourceCards[5];

    //enum Resource { NONE, WATER, FOREST, HILL, MOUNTAIN , ANY };

    resourceCard.setType( ANY );
    resourceCards[0] = resourceCard;
    resourceCard.setType( WATER );
    resourceCards[1] = resourceCard;
    resourceCard.setType( FOREST );
    resourceCards[2] = resourceCard;
    resourceCard.setType( HILL );
    resourceCards[3] = resourceCard;
    resourceCard.setType( MOUNTAIN );
    resourceCards[4] = resourceCard;


    Deck deck( resourceCards , 5 );


    cin.get();
    return 0;
}
However you can use polymorphism, and cast up. then slap a function in, so you can identify the card type;

Code: Select all

// in main function
Card *res = new ResourceCard;
// into class Card
virtual string type()
{
	return "Card";
}
// into class ResourceCard
string type()
{
	return "ResourceCard";
}