1

I am making a chess game and when I click on a square I want to know what piece is there. Since there are more than 1 type of pieces it would be annoying to have more variables in the Square structure.

So I have though about a class named Piece which is the parent of each type of Piece.

Example.

class Pawn : public Piece

I want to achieve a Square structure that looks something like this :

struct Square { Piece *piece };

Now, I want to initialize the piece variable like this :

piece = new Pawn(); // or what type of piece it should be.

My problem is that by doing this I can still only access Piece's class functions and not the Pawns ones.

How do I achieve such thing as having only 1 parent which can access everything his children have?

taigi tanaka
  • 299
  • 1
  • 4
  • 17
  • You have to define an interface in Piece, common to all your classes deriving from Piece, through virtual methods. http://stackoverflow.com/questions/2391679/can-someone-explain-c-virtual-methods – Antonio Aug 19 '13 at 00:28
  • 2
    learn about generic programming : http://www.cplusplus.com/doc/tutorial/templates and polymorphism : http://www.cplusplus.com/doc/tutorial/polymorphism – a.lasram Aug 19 '13 at 00:34
  • @a.lasram So, basically all I need to do is to define every child function in the base function and add the virtual word before it? – taigi tanaka Aug 19 '13 at 00:42
  • 1
    yes, do some experimentation to see for yourself and try understanding how virtual works, check this http://en.wikipedia.org/wiki/Virtual_method_table – a.lasram Aug 19 '13 at 00:48
  • 1
    @a.lasram ok, If you want to turn this into an answer, you got my accept :P thnx a lot. – taigi tanaka Aug 19 '13 at 00:55
  • Thanks, I'm glad my comments help but they do not represent an elaborated answer to your question and rather recommend an easy to follow tutorials around the question + you already have nice answers – a.lasram Aug 19 '13 at 01:08
  • You don't mention what functionality in Pawn you want to access through Piece. You may also be over-thinking things. What is wrong with making Piece an enum? Performance will be important if you want to create an AI for your game. – Neil Kirk Aug 19 '13 at 02:19
  • @NeilKirk First, I want my pieces to move :) Then I will think about performance ;) – taigi tanaka Aug 19 '13 at 02:37

2 Answers2

4

You can make virtual functions. Define virtual function in base class and override it in child class.

For Example

class Base
{
public:
    const char* SayHi() { return "Hi"; } // a normal non-virtual function    

    virtual const char* GetName() { return "Base"; } // a normal virtual function

    virtual int GetValue() = 0; // a pure virtual function
};
class Child:public Base{
{
    int GetValue(){
    //write any code here
    //return something;
    }
}

For more refer to link: http://www.learncpp.com/cpp-tutorial/126-pure-virtual-functions-abstract-base-classes-and-interface-classes/

Himanshu Pandey
  • 726
  • 5
  • 13
  • wouldn't that mean that I have to define a virtual function for every overrided function? I mean as far as I read I only need to define it right? – taigi tanaka Aug 19 '13 at 00:39
3

The best approach by far is try avoiding this situation: can you make the interface of the Piece uniform for all subclasses? If this can be done, choose this design without much hesitation, because what I describe below is a lot more complex, and also much harder to read.

Since you cannot access member functions of derived types through a pointer to the base type, you need to work around this in one of several ways:

  • Use dynamic_cast<Derived> - This is very simple, but extremely fragile. Changes to the inheritance structure can break an approach based on frequent dynamic casts.
  • Use VisitorPattern - This is a good choice when the class structure is not expected to change (you're in luck here: the list of types that you plan to model has not changed in many centuries)
  • Use Runtime Type Information and maps of function objects - This approach is very flexible, but it is somewhat hard to read. Lambdas of C++11 make it easier, though.
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Ok, Yep they seem pretty complicated but VisitorPattern seems to do what I want but still very complicated. I basically want that when I click on a square to have a variable holding whatever Piece is there, therefore I need a variable type that can be a Pawn, a bishop , a rook anything from the possible pieces list. My only idea was some sort of inheritance, I have also though about making only 1 functions but that is a lot if ifs and elses. Any other idea? – taigi tanaka Aug 19 '13 at 00:38
  • @taigitanaka Why do you need a variable to be of type `Pawn`, `Rook` or whatever? Can you not put the functions into `Piece` so that it is not important what specific piece it is? For example, you could define functions `list attacksSquares()`, `bool canCapturePieceAt(int r, int c)` and so on, and then override them in each piece providing the specific implementation. – Sergey Kalinichenko Aug 19 '13 at 00:42
  • Well, each piece has a diffrent model, and can move to diffrent places, wouldn't it be a pain to put all those in a single class? I mean i think the code would look pretty bad. – taigi tanaka Aug 19 '13 at 00:45
  • @taigitanaka: The point is, you're not putting the model and code all in one class. You're setting up `Piece` as an abstract class which just lists functions you can do to any piece, then implementing those same functions with different code for each concrete class like `Pawn`. – aschepler Aug 19 '13 at 00:51
  • Yep,That's what I'm gonna do but I've learned about them just now thnx to a.lasram and his/her links xD – taigi tanaka Aug 19 '13 at 00:54