operator->

在学习Stl的过程中,经常看到->符号的重载,但一直不太明白。

今天做了一个小测试,来看看如果调用它。

以list的迭代器为例,在

pointer operator->() const { return &(operator*());}

中加入 cout<<”test”<<endl;

然后编写如下代码:

    string str="my god";
    cout<<str.size()<<endl;//6
    list<string> str_list;
    str_list.push_back(str);
    list<string>::iterator it=str_list.begin();
    cout<<it->size()<<endl;//6

运行结果为:

6
test
6
从运行结果可知:”cout<<it->size()<<endl;”调用了重载的”->”,它返回一个string类的指针,然后再调用string的size()函数。
posted @ 2014-05-13 21:19  sxcww  阅读(188)  评论(0编辑  收藏  举报