我有以下問題:假設類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
}
這甚至可能嗎?
這不會在這裏工作,'operator >>'不是基類成員,而是一個自由函數。 – Xeo 2011-12-31 11:26:56
哦謝謝:),有點困惑。 – Kos 2011-12-31 11:35:48