ITeye博客

c++ 操作符重载 io操作符重载

nash01 · 2011-07-31 16:12 · c++ · 编程语言 · Markdown

1.io操作符的重载函数必须为非成员函数,否则左操作数最能是该类类型的对象:


Shop_item item;
item << cout;

2.io操作符通常对非共用数据进行读写,因此,通常将io操作符设为友员。


class Shop_item {
    friend std::istream& operator>>(std::istream&, Shop_item&);
    friend std::ostream& operator<<(std::ostream&, Shop_item&);
public:
    Shop_item&  operator+=(const Shop_item&);
}

在这个类中第三,四行为两个非成员操作符,第五行为一个成员操作符。 可以看到输入输出操作符为非成员操作符,并且在Shop_item这个类中输入输出操作符为友员。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。