クラスIPrinterManager
およびその子クラスColorPrinterManager
が指定されています。 ColorPrinter
は、子クラスのAbstractPrinter
です。コンパイルはメッセージColorPrinterManager::print is not a covariant of IPrinterManager::print
で中止されます。この問題を解決するにはどうすればよいですか?子クラスの戻り値型のオーバーライド
class IPrinterManager
{
public:
virtual std::vector<std::shared_ptr<AbstractPrinter>>* print(std::string text) = 0;
};
class ColorPrinterManager : public IPrinterManager
{
public:
std::vector<std::shared_ptr<ColorPrinter>>* print(std::string text);
};
しかし、カラープリンタだけを返すようにしたいと考えています。それ以外の方法はありませんか? – user1056903
@ user1056903、あなたの実装はそれを強制する方法であり、クライアントは気にする必要はありません。 'AbstractPrinter'が適切なインターフェースを提供しないためにこれをしたいのであれば、デザインを再考する必要があります。 – StoryTeller