class A

{

    protected:

        int a;

    public:

        A(int n){a = n;}

        void f(){std::cout<<"f is invoked!"<<'\n';}

};

int main(int argc, char *argv[])

{   

    A* pp = new A(4);

    std::auto_ptr<A> p(pp);

    p->f();

    p.operator->()->f();

    return 0;

}

程序执行时p->f()和p.operator->()->f()的结果一样,看了auto_ptr模板类的实现,发现其中对->操作符的定义如下:

  _Tp* operator->() const __STL_NOTHROW {

    return _M_ptr;

  }

不理解的是p->f()这个调用是如何做到的?难道在调用时做了p到p.opertaor->()的隐式转换?但在auto_ptr模板类的定义中没有看到opertor _TP*()的定义啊。

--

x->m

x.operator->()->m

x.operator->().operator->()->m

..

you are right

【 在 devilphoenix (初號機·神·小白) 的大作中提到: 】

: An expression x->m is interpreted as (x.operator->())->m for a class object x of type T if T::operator->() exists and if the operator is selected as the best match function by the overload resolution mechanism (13.3).

: 没看懂你是啥意思