私は次のような問題があります:クラスItem
が製品のシリアル番号を保持しているとします。 クラスBook
はItem
のシリアル番号を継承するItem
です。すべてのクラスにoperator>>
を作成して使用する必要があります。私はoperator>>
からItem
までを作成してから、その本のistream
の実装でそれを呼び出すことを考えましたが、どのようにすればよいかわかりません。演算子>>を継承できますか?
コードは次のようになります:
class Item
{
protected:
int _sn;
public:
Item();
~Item();
...
const istream& operator>>(const istream& in,const Item& x)
{
int temp;
in>>temp;
x._sn=temp;
return in;
}
};
class Book
{
private:
char _book_name[20];
public:
Book();
~Book();
...
const istream& operator>>(const istream& in,const Book& x)
{
char temp[20];
////**here i want to use the operator>> of Item**////
in>>temp;
strcpy(x._book_name,temp);
return in;
}
};
int main()
{
Book book;
in>>book; //here i want to get both _sn and _book_name
}
これはさえ可能ですか?
ここでは動作しません。演算子>>は基本クラスのメンバーではなく、自由な関数です。 – Xeo
ああありがとう:)、ちょっと混乱しました。 – Kos