我有一个PieceStrategy类:
#include "QueenStrategy.cpp"
class PieceStrategy {
void promoteToQueen() {
this = new QueenStrategy();
}
}
我有一个继承自它的QueenStrategy类:
#include "PieceStrategy.cpp"
class QueenStrategy : public PieceStrategy {}
现在出现循环包含问题。但在这种情况下,我不能使用前向声明。
我该怎么办?
答案 0 :(得分:4)
this
piece.hpp
#include "strategy.hpp"
class Piece
{
std::unique_ptr<Strategy> strategy;
public:
static Piece Pawn();
void PromoteToQueen();
};
piece.cpp
#include "pawn.hpp"
#include "queen.hpp"
Piece Piece::Pawn()
{
Piece p;
p.strategy = std::make_unique<PawnStrategy>();
return p;
}
void Piece::PromoteToQueen()
{
strategy = std::make_unique<QueenStrategy>();
}