Question about polymorphism (or so I believe)

A

Aaron

I want to build two abstract base classes.
1) Game
2) Position

The Game object represents a boardgame in its entirety and the
Position object represents individual game states. The Game object
will contain an array of Position objects representing the history of
the game. Each specific game will be expressed as a derivative of
these two classes. What I want to do is declare the base class array
in such a way that the derived classes don't have to redeclare this
array every time to the correct type.

This question of course stems from a lack of experience I have with C+
+ and many years of experience with scripting where typing is just not
an issue. My understanding is that in the base class I could declare
this array like this:
vector<Position> pos_hist

But my meager understanding also tells me that the derivative class
Chess::Game would then have to redeclare this member as:
vector<Game::position> pos_hist

Is there a way to make this redeclaration unnecessary, even if the
derived Position classes will almost certainly have to add new members
and methods?

Thank you for your time and patience.
 
V

Victor Bazarov

Aaron said:
I want to build two abstract base classes.
1) Game
2) Position

The Game object represents a boardgame in its entirety and the
Position object represents individual game states. The Game object
will contain an array of Position objects representing the history of
the game. Each specific game will be expressed as a derivative of
these two classes. What I want to do is declare the base class array
in such a way that the derived classes don't have to redeclare this
array every time to the correct type.

This question of course stems from a lack of experience I have with C+
+ and many years of experience with scripting where typing is just not
an issue. My understanding is that in the base class I could declare
this array like this:
vector<Position> pos_hist

But my meager understanding also tells me that the derivative class
Chess::Game would then have to redeclare this member as:
vector<Game::position> pos_hist

Is there a way to make this redeclaration unnecessary, even if the
derived Position classes will almost certainly have to add new members
and methods?

Thank you for your time and patience.

If you're looking to create a "polymorphic" container (or a container
of objects that you could use polymorphically), then you need to store
_pointers_ to the base class in that vector. If you just store the
base class objects, you will slice all the relevant parts when trying
to place the derived objects into that vector.

V
 
A

Aaron

--SNIP--

If you're looking to create a "polymorphic" container (or a container
of objects that you could use polymorphically), then you need to store
_pointers_ to the base class in that vector. If you just store the
base class objects, you will slice all the relevant parts when trying
to place the derived objects into that vector.

So this is where the whole void pointer thing comes in? Thank you for
your help. I appreciate it.

Aaron Dalton
http://superdupergames.org
 
V

Victor Bazarov

Aaron said:
So this is where the whole void pointer thing comes in?

*Void* pointer? Definitely not. It has to be a pointer to the base
class.

V
 
A

Aaron

*Void* pointer? Definitely not. It has to be a pointer to the base
class.

But if the derived class has made modifications that change the amount
of memory it consumes, and I store pointers to the base class instead
of to the derived one...hrm, obviously I'm missing something. I will
implement it using pointers to the base class and wait for the library
to get that book in so I can refresh the whole pointer thing in my
head.

Thanks again.
Aaron
http://superdupergames.org
 
V

Victor Bazarov

Aaron said:
But if the derived class has made modifications that change the amount
of memory it consumes, and I store pointers to the base class instead
of to the derived one...hrm, obviously I'm missing something. I will
implement it using pointers to the base class and wait for the library
to get that book in so I can refresh the whole pointer thing in my
head.

Make sure that you don't mix up addresses of dynamic objects with those
of static or automatic objects in that collection.

You have another option - a virtual function in the base 'Game' class
that should return the requested 'Move' object (by reference or pointer)
to be processed. That would put the burden of storing and maintaining
the collection of 'Move' objects (or their derived variations) on the
derived [from 'Game'] class. Example

class Move {
public:
virtual char const* title() const = 0;
};

#include <iostream>
#include <cstring>

class Game {
virtual const Move& getMove(size_t ind) const = 0;
virtual size_t moveCount() const = 0;
public:
void dump(std::eek:stream& os) const {
for (size_t i = 0; i < moveCount(); ++i)
os << getMove(i).title() << std::endl;
}
};

class MyChessMove : public Move {
char const* title_;
char const* title() const { return title_; }
MyChessMove(const char* t) : title_(t) {}
friend class MyChessGame;
};

#include <vector>

class MyChessGame : public Game {
std::vector<MyChessMove> history;
public:
MyChessGame() { history.push_back("e2-e4");
history.push_back("I give up");
}
const Move& getMove(size_t ind) const { return history[ind]; }
size_t moveCount() const { return history.size(); }
};

class MyBlackjackMove : public Move {
size_t value;
const char* title() const {
char c = "_A23456789xJQK"[value % 14];
char const *suit[] =
{ "Spades", "Clubs", "Diamonds", "Hearts" };
static char what_of_what[32];
what_of_what[0] = c; what_of_what[1] = 0;
strcat(what_of_what, " of ");
strcat(what_of_what, suit[value / 100]);
return what_of_what;
}
MyBlackjackMove(size_t card, size_t suit)
: value((suit % 4)*100 + card % 14) {}

friend class MyBlackjackGame;
};

#include <list>

class MyBlackjackGame : public Game {
std::list<MyBlackjackMove> hand;
public:
MyBlackjackGame() {
hand.push_back(MyBlackjackMove(1, 10));
hand.push_back(MyBlackjackMove(2, 12));
}
const Move& getMove(size_t ind) const {
std::list<MyBlackjackMove>::const_iterator it = hand.begin();
return std::advance(it, ind), *it;
}
size_t moveCount() const { return hand.size(); }
};

int main() {
MyChessGame chess;
MyBlackjackGame blackjack;

chess.dump(std::cout);
blackjack.dump(std::cout);
}

Enjoy!

V
 
T

terminator

But if the derived class has made modifications that change the amount
of memory it consumes, and I store pointers to the base class instead
of to the derived one...hrm, obviously I'm missing something. I will
implement it using pointers to the base class and wait for the library
to get that book in so I can refresh the whole pointer thing in my
head.

Thanks again.
Aaronhttp://superdupergames.org

declare the destructors as virtual and use pointers to base and do not
worry about the size:

struct position{
virtual void my_abstract()=0;// pure/abstract function
...//class stuff
virtual ~position(){/*destruction(finalizer) stuff here*/};
};

regards,
FM
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top