473,486 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Question about polymorphism (or so I believe)

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
6 1396
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
1940
by: Vincezo Ciaschini | last post by:
Supposing you have the following declaration in a header file: -- head.h -- struct s { int c; char v; #if defined(__cplusplus) s(); s(double); method1(int);
12
7008
by: Steve Jorgensen | last post by:
The classing Visual Basic and VBA support for polymorphism, let's face it, is a bit on the weak side, and built-in support for inheritance is non-existent. This little essay is about some patterns...
41
1580
by: GC | last post by:
Within a class, do you use the private keyword for your internal member variables or do you leave it off like you would for variables within a function?
3
858
by: JB | last post by:
So I am beginning to dabble in the express editions that MS is offering currently. I am trying to write a basic program, but am having issues. Here is the code so far: Public Class testprog ...
5
1145
by: pvdg42 | last post by:
VS 2003, C# if the language matters. Scenario: Two page project, text box content on the start page is loaded into session variables to be displayed in a greeting on the second page. Works fine...
18
3833
by: Seigfried | last post by:
I have to write a paper about object oriented programming and I'm doing some reading to make sure I understand it. In a book I'm reading, however, polymorphism is defined as: "the ability of two...
2
3364
by: sarathy | last post by:
Hi all, I need a small clarification reg. Templates and Polymorphism. I believe templates is really a good feature, which can be used to implement generic functions and classes. But i doubt...
3
2703
by: ryan | last post by:
Lets say i have a database where users can upload their dvd collection. I want to be able to do a query where i can select all the users who have a certain DVD. Can someone point me in the right...
1
1229
by: Amaxen1 | last post by:
Hello all, I have a general architecture question. We have a system that essentially does multiple processes/operations against a single table. The objective is to increase performance (i.e....
5
1368
by: Computer Guy | last post by:
I know this is not actually a PHP question, but I figured this would be the place to go to get a good answer. I have a website for the neighborhood I live in and people's main concern is security....
0
7100
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7126
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7175
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5434
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3070
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
262
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.