有没有办法获取重载operator->的返回值而不是其成员值?

假设我有一个带有重载 operator-> 的类,它返回一个指针。但是, -> 的用法在它之后需要成员,但我可以不提供成员吗?有没有办法直接获取返回值?

下面的代码片段展示我的问题:我想知道如果有一种方式来获得的值ptrWrapper通过类的operator->

#include <iostream>

using namespace std;

struct Entry{
    // could have other private fields, so `a` may not be the head of Entry.
    int a;
    int b;
};

class Wrapper {
public:
    Wrapper(Entry* p): ptr(p) {}
    
    Entry* operator->() {
        return ptr;
    }
    
    
private:
    Entry* ptr;
};


int main()
{
    Entry entry {12, 34};
    Wrapper wrapper(&entry);
    cout << wrapper->a << "n";
    
    // is there a way to get wrapper->ptr from the operator-> directly?
}

回答

就在这里。你可以简单地做,例如:

cout << wrapper.operator->();


以上是有没有办法获取重载operator-&gt;的返回值而不是其成员值?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>