简体   繁体   English

从超类对象,虚拟函数中调用子类方法

[英]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. 我正在设计一个国际象棋游戏(C ++初学者),但是我只能检查单个棋子类型,例如典当一次只能移动1个空格,第一次则只能移动2个空格,依此类推。

I have a piece class, and a subclass of that is pawn, king, etc, which contains the method: 我有一个片断类,而它的子类是pawn,king等,其中包含以下方法:

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: 初步检查后,我想从main调用上面的check函数作为测试:

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. 这给了我一个错误“没有名叫'check'的成员”的错误,这是有道理的,但是我敢肯定会有一种方法将片断类链接到典当,国王等。

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. 我仍然希望将pawn类的check方法保持在同一位置,因为我认为它可以很好地封装它。

EDIT: I made a pure virtual function in the piece class: 编辑:我在piece类中做了一个纯虚函数:

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. 现在,当我调用上面的cout行时,出现了细分错误,并且不确定原因。 I'm assuming it's because I'm using pointers? 我假设这是因为我在使用指针?

EDIT2: Thank you for that! EDIT2: 谢谢你! I have implemented this however I got a small error, is virtual meant to be attached to the pawn and king class? 我已经实现了,但是我遇到了一个小错误,虚拟意味着要附加到Pawn和King类吗? 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. EDIT3:我了解,我使用虚拟标记标记了pawn和king类中的check函数,并对其进行了编译。

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! EDIT4:现在一切正常,我正在从main调用它进行测试,但是当我从程序中调用它时,一切正常,谢谢大家!

What you are attempting to do is exactly what virtual methods are meant for. 您试图做的正是virtual方法的含义。

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 ...;
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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