C ++中的Abstract / Base结构?

时间:2012-01-27 06:02:23

标签: c++ class struct abstract-class structure

我正在制作国际象棋游戏,我希望有一系列作品。

如果我是正确的,在Java中你可以有一个抽象的Piece类,并且KingQueen扩展该类。如果我要制作一个Piece的数组,我可以在该数组中的某处放置一个King件,在另一个位置放置一个Queen件,因为King和{{1} } Queen

有没有办法用C ++中的结构做到这一点?

6 个答案:

答案 0 :(得分:6)

是。您可以在C ++中创建abstract base class。只需将一个或多个方法设置为纯虚拟:

class Piece {
    public:
        Piece ();
        virtual ~Piece ();
        virtual void SomeMethod () = 0;  // This method is not implemented in the base class, making it a pure virtual method. Subclasses must implement it
 };

然后,您可以创建实现纯虚方法的子类

class King : public Piece {
    // ... the rest of the class definition
    virtual void SomeMethod (); // You need to actually implement this
};

答案 1 :(得分:4)

您可以通过向类添加纯虚函数来在C ++中模拟 Abstract class

在c ++中你可以使用你可以上课的结构来做所有事情 C ++中的结构仅在成员的默认访问规范和继承期间与类不同。

答案 2 :(得分:3)

不确定

class AbstractPiece
{
public:
virtual void aFunction()=0; //this makes AbstractPiece an abstract class.
virtual ~AbstractPiece();
};

class King : public AbstractPiece
{
public:
virtual void aFunction(); //this is the one that would get called
virtual ~King();
};

然后,稍后,

AbstractPiece* array[size];

array[index] = new King();

答案 3 :(得分:1)

您可以在C ++中使用继承,就像在Java中一样。 C ++有类和结构,在这种情况下你可以使用它们。

C ++中的抽象类只是包含纯虚方法的类。这些是没有方法定义的方法。

您的解决方案可能如下所示:

class Piece {
public:
   virtual ~Piece ();
   virtual void move() = 0; //this is a pure virtual function, making this an abstract class.
};

class King : public Piece {
public:
   void move() {};
};

class Queen : public Piece {
public:
  void move() {};
};

....

Piece *myPieces[2] = new Piece*[2];
myPieces[0] = new King();
myPieces[1] = new Queen(); 

答案 4 :(得分:1)

在c ++中,您可以使用抽象类或结构:

#include <iostream>
#include <vector>
#include <algorithm>
#include <boost/lambda/bind.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/construct.hpp>

using namespace std;

struct piece
{
    virtual ~piece() {};
    virtual void dosomething() = 0;
};

struct king : piece
{
    ~king() { cout << __PRETTY_FUNCTION__ << endl; }
    void dosomething() { cout << __PRETTY_FUNCTION__ << endl; }
};

struct queen : piece
{
    ~queen() { cout << __PRETTY_FUNCTION__ << endl; }
    void dosomething() { cout <<  __PRETTY_FUNCTION__ << endl; }
};

struct pawn : piece
{
    ~pawn() { cout << __PRETTY_FUNCTION__ << endl; }
    void dosomething() { cout << __PRETTY_FUNCTION__ << endl; }
};

用法:

int main()
{
    typedef std::vector< piece * > pieces_t;
    pieces_t pieces;
    pieces.push_back(new queen());
    pieces.push_back(new king());
    pieces.push_back(new pawn());
    pieces.push_back(new pawn());
    pieces.push_back(new pawn());
    pieces.push_back(new pawn());

    // calling dosomething()
    std::for_each(pieces.begin(), pieces.end(), 
          boost::lambda::bind(&piece::dosomething, boost::lambda::_1));

    // destructing
    std::for_each(pieces.begin(), pieces.end(), 
          boost::lambda::bind(boost::lambda::delete_ptr(), boost::lambda::_1));
}

输出:

virtual void queen::dosomething()
virtual void king::dosomething()
virtual void pawn::dosomething()
virtual void pawn::dosomething()
virtual void pawn::dosomething()
virtual void pawn::dosomething()
virtual queen::~queen()
virtual king::~king()
virtual pawn::~pawn()
virtual pawn::~pawn()
virtual pawn::~pawn()
virtual pawn::~pawn()

答案 5 :(得分:1)

我更愿意避免使用指针,因为你必须记得要清理它们。

此设计使用策略模式。界面为PieceType,允许您在电路板上显示有效位置,或显示电路板上的初始位置。我相信你会希望每个策略都能做到更多:

class Board;

class PieceType
{
public:
    virtual showValidMoves(Board& board) const = 0;
    virtual showInitialPosition(Board& board) const = 0;
    // ...  
};

class Pawn : public PieceType
{
public:
    virtual showValidMoves(Board& board) const;
    virtual showInitialPosition(Board& board) const;
    // ...
};

class Rook : public PieceType
{
    // ...
};
//...

我们只需要每个PieceType中的一个,并且因为该类型在西洋棋棋子中共享,它也可以是const:

const Pawn PAWN;
const Rook ROOK;
const Knight KNIGHT;

我们使用这些策略来初始化我们保存在矢量中的西洋棋棋子:

class ChessMan
{
public:
    enum Colour {Black, White};

    ChessMan(const Colour& colour, PieceType& pieceType);

    void showValidMoves(Board& board);
    void showInitialPosition(Board& board);
private:
    Colour m_colour;
    PieceType& m_pieceType;
};

我将在自己的函数中包含用于向向量添加ChessMan的代码:

void add(vector<ChessMan>& chessmen,
         const ChessMan::Colour& colour,
         const PieceType& pieceType,
         const unsigned int amount)
{
    chessmen.insert(chessmen.end(), ChessMan(colour, pieceType), amount);
}

void main()
{
    using std::vector;

    vector<ChessMan> chessmen;
    add(chessmen, 16, ChessMan::Black, PAWN);
    add(chessmen, 2, ChessMan::Black, ROOK);
    add(chessmen, 2, ChessMan::Black, KNIGHT);
    //...
}

Bonne chance!