如何创建和使用类箭头运算符? [英] How do I create and use a class arrow operator?

查看:72
本文介绍了如何创建和使用类箭头运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在对其进行了所有研究之后,我似乎找不到如何创建类箭头运算符的方法,即

So, after researching everywhere for it, I cannot seem to find how to create a class arrow operator, i.e.,

class Someclass
{
  operator-> ()  /* ? */
  {
  }
};

我只需要知道如何使用它并正确使用它. -它的输入是什么? -它返回什么? -如何正确声明/制作原型?

I just need to know how to work with it and use it appropriately. - what are its inputs? - what does it return? - how do I properly declare/prototype it?

推荐答案

运算符->用于重载成员访问权限.一个小例子:

The operator -> is used to overload member access. A small example:

#include <iostream>
struct A 
{
    void foo() {std::cout << "Hi" << std::endl;}
};

struct B 
{
    A a;
    A* operator->() {
        return &a;
    }
};

int main() {
    B b;
    b->foo();
}

这将输出:

Hi

这篇关于如何创建和使用类箭头运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆