编程语言
首页 > 编程语言> > 智能指针中C++重载'->'符号是怎么实现的

智能指针中C++重载'->'符号是怎么实现的

作者:互联网

例如下面的代码:

class StrPtr{
public:
    StrPtr() : _ptr(nullptr){}
    //拷贝构造函数等省略...
    std::string* operator->()
    {
        return _ptr;
    }
private:
    std::string *_ptr;
};

std::string* operator->()
/*
这句代码的格式不是类似于前导运算符吗?类似于std::string operator*(){//...}不正是*ptr?但是重载的->运算符却是ptr->这样使用的,请问是为什么?
而且std::string* operator->()返回的是指针,为什么可以直接在后面访问类成员,[比如说ptr->size()]?
我的疑问是这是如何实现的,这和我对运算符重载的直接理解有所差异。
*/

解答:

这句代码的格式不是类似于前导运算符吗?类似于std::string operator*(){//...}不正是*ptr?但是重载的->运算符却是ptr->这样使用的,请问是为什么?

因为运算符的结合律不同,dereference operator(*)是 right associative 的,而 member access operator(.->)是 left associative 的。这是不同符号的结合律不同的例子,C++ 里符号相同的时候也会有用法不同导致结合律不同的例子。例如:

 operator | left associative            | right associative
          |   lhs op rhs  /  lhs op     |   op rhs
----------+-----------------------------+---------------------------
 ++ --    | postfix increment/decrement | prefix increment/decrement
 + -      | binary add/subtract         | unary plus/minus
 *        | binary multiply             | dereference
 &        | bitwise and                 | address-of
 ()       | function call               | type conversion

当你重载一个左结合律的操作符时(如+ - * / ()等),往往这个操作符是个二元操作符,对于lhs op rhs,就会调用lhs.operator op(rhs)operator op(lhs, rhs),我们只需要按照这个函数签名来重载操作符就行了。

但是,如果这个操作符是一元操作符怎么办?这就要分情况讨论了:

所以会让人有点糊涂为啥函数签名差不多,用法却不同。

而且std::string* operator->()返回的是指针,为什么可以直接在后面访问类成员,[比如说ptr->size()]?

这是由 C++ 标准规定的,对于ptr->mem根据ptr类型的不同,操作符->的解释也不同:

你会发现这是一个递归的解释,对于ptr->mem会递归成:

(*(ptr.operator->().operator->().….operator->())).mem

操作符->是一元的,而操作符.是二元的。操作符->最终是通过操作符.来访问成员的,而.这个操作符是不允许重载的,只能由编译器实现。

举个例子,使用题主定义的类StrPtr

string s = "abc";
StrPtr ptr(&s);
string *sp = &s;

ptr->size();
// 等价于 ptr.operator->()->size();
// 等价于 _ptr->size();   这跟 sp->size();   不就一样了吗
// 等价于 (*_ptr).size(); 这跟 (*sp).size(); 不就一样了吗

最后一句题外话,C++ 变量名不要用下划线作为起始,用下划线做起始是保留给编译器使用的。可以使用m_somemembersomemember_作为私有成员名称。

 

标签:string,C++,operator,lhs,重载,操作符,指针,ptr,op
来源: https://www.cnblogs.com/zhjblogs/p/15189335.html