博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
operator ->
阅读量:7226 次
发布时间:2019-06-29

本文共 1110 字,大约阅读时间需要 3 分钟。

 

//The arrow operator has no inputs. Technically, it can return whatever you want, but it should return something that either is a pointer or can become a pointer .

 

//The -> operator automatically dereferences its return value before calling its argument using the built-in pointer dereference, not operator*, so you could have the following class:

class PointerToString{    string a;public:    class PtPtS    {    public:        PtPtS(PointerToString &s) : r(s) {}        string* operator->()        {            std::cout << "indirect arrow\n";            return &*r;        }    private:        PointerToString & r;    };    PointerToString(const string &s) : a(s) {}    PtPtS operator->()    {        std::cout << "arrow dereference\n";        return *this;    }    string &operator*()    {        std::cout << "dereference\n";        return a;    }};

这个类可以这么用 

PointerToString ptr(string("hello"));string::size_type size = ptr->size();

上面语句 也可以转换为

string::size_type size = (*ptr.operator->().operator->()).size();

分析这个问题 主要是因为 看stl代码时发现了 auto_ptr 中对其的重载

转载于:https://www.cnblogs.com/jijiboy/p/10177478.html

你可能感兴趣的文章
halt
查看>>
标准ACL+扩展ACL+命名ACL
查看>>
Meteor应用的启动过程分析
查看>>
九曲黄河万里沙,浪淘风簸自天涯 — 正则表达式
查看>>
欲哭无泪,联想笔记本性价比
查看>>
很简单的在Ubuntu系统下安装字体和切换默认字体的方法
查看>>
我的友情链接
查看>>
dojo框架用hitch实现函数与上下文的绑定
查看>>
ubuntu编译安装vim7.4
查看>>
python之利用PIL库实现页面的图片验证码及缩略图
查看>>
IP-COM设置×××
查看>>
VPC配置案例
查看>>
十年IT运维谈(五):要专业化还是平台化?
查看>>
分享超级给力的一个外发光Shader
查看>>
oblog_4.6_SQL 语句
查看>>
通过Git WebHooks+脚本实现自动更新发布代码之shell脚本
查看>>
对象实例化、字符串的使用方法
查看>>
keepalived基于LVS实现高可用,实现web服务的高可用
查看>>
80端口被Microsoft-HTTPAPI/2.0占用的解决办法
查看>>
无法抗拒Minecraft给予超高的自由度和探索-微访谈
查看>>