Polymorphism and Arrays

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Maevik
Chaos Rift Junior
Chaos Rift Junior
Posts: 230
Joined: Mon Mar 02, 2009 3:22 pm
Current Project: www.keedepictions.com/Pewpew/
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: Long Beach, CA

Polymorphism and Arrays

Post 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.)
My love is like a Haddoken, it's downright fierce!
User avatar
adikid89
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 94
Joined: Tue Apr 27, 2010 6:59 am
Current Project: small tiny-mini projects
Favorite Gaming Platforms: PC I guess...
Programming Language of Choice: c++

Re: Polymorphism and Arrays

Post by adikid89 »

use a vector of dynamically allocated Cards? vector<Card*> cards; Since they're pointers polymorphism will work.
My first game C++/SDL Yoshi Combat! = http://www.youtube.com/watch?v=HQ9mMBEWSZg
==============================================================
Image
User avatar
thejahooli
Chaos Rift Junior
Chaos Rift Junior
Posts: 265
Joined: Fri Feb 20, 2009 7:45 pm
Location: London, England

Re: Polymorphism and Arrays

Post 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
I'll make your software hardware.
D-e-X
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 39
Joined: Tue Dec 28, 2010 6:49 pm

Re: Polymorphism and Arrays

Post 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 ;)
I remember when I used to be into nostalgia.
User avatar
thejahooli
Chaos Rift Junior
Chaos Rift Junior
Posts: 265
Joined: Fri Feb 20, 2009 7:45 pm
Location: London, England

Re: Polymorphism and Arrays

Post 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.
I'll make your software hardware.
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: Polymorphism and Arrays

Post 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";
}
Post Reply