override operator <<

本文介绍了一个C++中运算符重载的具体实例,展示了如何为自定义类型实现<<运算符,以便于对象的输出。通过遍历并打印容器内的元素,使输出更加直观。同时强调了正确的参数顺序及必要的头文件包含。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

19.override operator <<
            struct topic_descript
            {
                btstring topic_url;     //RuleEngine/LineDetector/Crossed
                btstring name_space;
                btvector<field_item> source_items;
                btvector<field_item> key_items;      
                btvector<field_item> data_items;

                friend std::ostream& operator <<(std::ostream& ou,topic_descript& obj){
                    ou<<"topic_url: "<<obj.topic_url<<"\n";
                    ou<<"name_space: "<<obj.name_space<<"\n";
                    btvector<field_item>::iterator it;
                    for(it=obj.source_items.begin(); it!=obj.source_items.end(); ++it){
                        ou<<*it<<"\n";
                    }
                    for(it=obj.key_items.begin(); it!=obj.key_items.end(); ++it){
                        ou<<*it<<"\n";
                    }
                    for(it=obj.data_items.begin(); it!=obj.data_items.end(); ++it){
                        ou<<*it<<"\n";
                    }
                    return ou;
                }               
            };
注意: 找不到ostream 是由于 
1.没包含 #include <iostream> 2.或者命名空间 std的 缘故。
C++ 就是比 C麻烦的多,  不会有 namespace, 找到就是找到,  找不到就是 undefined!
3. the order:
    friend std::ostream& operator <<(std::ostream& ou,topic_descript& obj)
    not like this
    friend std::ostream& operator <<(topic_descript& obj,std::ostream& ou),
    like that errors  happened!
建立一个继承体系,List 是基类,ArrayList 和 LinkedList 是其派生类。并且编写实现下述函数并达到如下效果。 ostream& operator << (ostream&os, const List&rhs); 做一个流输出运算符重载,其第二个参数是List的常引用类型。我们知道子类的对象天生可以作为父类类型使用,因此 ArrayList a; LinkedList b; operator << (cout,a); operator << (cout,b); 这上面的调用显然都是合法的。但是现在要求实现如下效果:第 3 行的函数执行的是适合 ArrayList 输出的代码,而第 4 行执行的是适合 LinkedList 输出的代码。即,虽然调用的函数一样,但需要根据当时的实参类型选择合适的实现。相当于对非成员函数做到动态绑定。 相关知识 前面说过,虚函数可以实现动态绑定。也就是: class List{ public: virtual void disp(ostream&os)const; }; class ArrayList : public List{ public: void disp(ostream&os)const{/*这里是ArrayList的实现*/} } class LinkedList : public List{ public: void disp(ostream&os)const{/*这里是LinkedList的实现*/} } List *p = new ArrayList; p->disp(cout);//此时调用的是ArrayList的disp函数 delete p; p = new LinkedList; p->disp(cout);//此时调用的是LinkedList的disp函数 delete p; 如果是非成员函数,能不能实现类似效果呢?即 ostream& operator << (ostream&os, const List&rhs); ostream& operator << (ostream&os, const ArrayList&rhs); ostream& operator << (ostream&os, const LinkedList&rhs); List *p = new ArrayList; cout<<*p<<endl;//此时希望调用的是operator<<(ostream,ArrayList&) delete p; p = new LinkedList; cout<<*p<<endl;//此时希望调用的是operator<<(ostream,LinkedList&) delete p; 遗憾的是,非成员函数不具备这样的功能。对于上述流输出运算符调用而言,它的实参会被看作是 List 类型,因此只会调用上文中的第一个重载函数。而不会根据 p 实际指向的类型动态调用其他流输出重载函数。 如何令非成员函数也体现出动态绑定的特性呢?答案是通过成员函数,而不是通过函数重载。例如,可以这样实现流输出运算符重载: ostream& operator << (ostream&os,const List&rhs){ rhs.disp(os); return os; } /************************************************** *这里不再需要实现 * ostream& operator << (ostream&os, const List&rhs); *和 * ostream& operator << (ostream&os, const List&rhs); *等函数 *****************************************************/ 编程要求 根据提示,在右侧编辑器的Begin-End区域内补充代码。 测试说明 本关共有 8 个文件。其中 List.h 定义了 List 类及其成员函数。ListOp.h 和 ListOp.cpp 定义了与 List 相关的运算符重载。ArrayList.h 和 ArrayList.cpp 定义了 ArrayList 类及其成员函数。LinkedList.h 和 LinkedList.cpp 定义了 LinkedList 类及其成员函数。main.cpp 定义了主函数。 用户的任务是编写 List.h、ArrayList.cpp 和 LinkedList.cpp 文件,以使得 main.cpp 正确运行。 List.h 内容如下: #ifndef _LIST_H_ #define _LIS
最新发布
05-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值