By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,819 Members | 1,173 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,819 IT Pros & Developers. It's quick & easy.

Question about polymorphism (or so I believe)

P: n/a
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<Positionpos_hist

But my meager understanding also tells me that the derivative class
Chess::Game would then have to redeclare this member as:
vector<Game::Positionpos_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.

Jun 8 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Aaron wrote:
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<Positionpos_hist

But my meager understanding also tells me that the derivative class
Chess::Game would then have to redeclare this member as:
vector<Game::Positionpos_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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 8 '07 #2

P: n/a
On Jun 8, 1:55 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Aaron wrote:
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.
--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
Jun 8 '07 #3

P: n/a
Aaron wrote:
On Jun 8, 1:55 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>Aaron wrote:
>>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.

--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?
*Void* pointer? Definitely not. It has to be a pointer to the base
class.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 8 '07 #4

P: n/a
On Jun 8, 2:12 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
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?

*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

Jun 8 '07 #5

P: n/a
Aaron wrote:
On Jun 8, 2:12 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>>>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?

*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.
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::ostream& 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<MyChessMovehistory;
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<MyBlackjackMovehand;
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 8 '07 #6

P: n/a
On Jun 9, 12:39 am, Aaron <a...@daltons.cawrote:
On Jun 8, 2:12 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>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?
*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.
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

Jun 9 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.