简体   繁体   中英

Calling subclass methods from superclass object, virtual functions

I'm designing a chess game (C++ beginner) however I'm stuck on my checking process for individual piece types, eg pawn moving only 1 space at a time, except the first time moving 2 spaces, and so on.

I have a piece class, and a subclass of that is pawn, king, etc, which contains the method:

check(string position,string destination);

and return a boolean value whether it is possible to move to the destination.

I have a pointer to each piece which I have defined by doing:

pawn pawnPieces[16];
piece *pointer[16];

for (i=0;i<16;i++)
{
    pointer[i]=&pawnPieces[i];
}

After my initial checking, I want to call the check function above from main, as a test:

cout << pointer[1]->check("B1","C1") << endl;

This gives me the error "no member named 'check' in piece" which makes sense, however I'm sure there would be a way to link the piece class to the pawn, king etc.

I think I need to do something with virtual functions from what I have read, but I am not sure how to approach this. If anyone could offer a few pointers that would be much appreciated.

This approach of trying to call the subclass function from a pointer to the class above it may be fundamentally flawed, perhaps I need to modify my design to achieve this goal? I still want to keep the check method of the pawn class in the same position, as I believe it encapsulates it well.

EDIT: I made a pure virtual function in the piece class:

virtual bool check(string positionIN,string destination)=0;

Now when I call the cout line above, I get a segmentation fault and I'm unsure why. I'm assuming it's because I'm using pointers?

EDIT2: Thank you for that! I have implemented this however I got a small error, is virtual meant to be attached to the pawn and king class? From my understanding I thought the virtual tag only goes on the base class.

EDIT3: I understand, I tagged the check function in classes pawn and king with the virtual tag and it compiled.

Now I am getting a segmentation fault through calling the object itself

pawnPieces[1].check("B1","C1") 

and by calling the pointer to the object

pointer[1]->check("B1","C1") 

from main, and I am not sure why.

EDIT4: All working now, I was calling it from main to test, however when I called it from within my program everything worked, thank you all!

What you are attempting to do is exactly what virtual methods are meant for.

class piece
{
public:
    virtual bool check(string position, string destination) = 0;
};

class pawn : public piece
{
public:
    virtual bool check(string position, string destination)
    {
        return ...;
    }
};

class king : public piece
{
public:
    virtual bool check(string position, string destination)
    {
        return ...;
    }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM