Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Do I need virtual methods or not?

This topic is 5956 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m toying with the idea in my chess program of generating the legal moves in a different way than normal. I''m thinking about having a piece class, then creating child classes from the piece class, such as class Piece, class WhitePawn, class WhiteKnight, etc. Then legal move generation becomes something like...
void GenerateLegalMoves(vector<Move> & moves) {
    for(int i = 0; i < pieces[sideToMove].size(); i++)
Since each piece has different rules for it''s legal moves, I would have to implement each piece''s move generation in it''s own specific way. Since I''m having a vector of different types, I assume I''ll have to declare it as vector pieces[2]; and then I will be able to do something like pieces[WHITE].push_back(WhitePawn(E2)); right? I''m a little hazy on this issue. I remember that it''s possible to have an array of different kinds of things, but I don''t remember if you can do it like that, or if you can only do it with pointers. Fill me in if you know The other question I''d like answered is whether or not the generateLegalMoves(vector & moves) method for each individual piece class has to be virtual or if it can just be a regular method of the derived class. If it has to be virtual, are there any potential performance hits that I''ll have? If so, I might not want to do it this way, since move generation is one of the main operations in a chess playing program. Thanks for your help. Russell

Share this post

Link to post
Share on other sites
edit: forgot your first question; yes, you will need to use a vector of pointers, c++ only supports polymorphy(sp?) for pointers. (And since pointers doesnt have destructors you have to delete each vector entry when you you cleanup).

Yes you will need virtual methods for this.
I dont know enough about C++ to tell you how big the performance penalty is for virtual methods, but its there thats for certain(i think calling such a method involves a lookup in a hashed virtual method table, so the overhead wont become greater for an increasing number of virtual methods), but its *probably* neglible(sp?).

[edited by - ziphnor on June 29, 2002 1:02:33 PM]

Share this post

Link to post
Share on other sites
References also exhibit polymorphic behavior

My bad I always think of references as just glorified pointers....
What i should have said is that value types cant be used in this way.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!