C++ 如何创建和使用类箭头运算符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4928557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I create and use a class arrow operator?
提问by Codesmith
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?
我只需要知道如何使用它并适当地使用它。- 它的输入是什么?- 它返回什么?- 我如何正确声明/原型化它?
回答by Daniel Gallagher
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 through chained ->
operators.
箭头运算符没有输入。从技术上讲,它可以返回任何你想要的东西,但它应该返回一些要么是一个指针,要么可以通过链式->
操作符变成一个指针。
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:
该->
运营商调用它的参数之前自动取消引用它的返回值使用内置的指针引用,没有operator*
,所以你可以有以下类:
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;
}
};
Use it like:
像这样使用它:
PointerToString ptr(string("hello"));
string::size_type size = ptr->size();
which is converted by the compiler into:
由编译器转换为:
string::size_type size = (*ptr.operator->().operator->()).size();
(with as many .operator->()
as necessary to return a real pointer) and should output
(尽可能多.operator->()
地返回一个真正的指针)并且应该输出
arrow dereference
indirect dereference
dereference
Note, however, that you can do the following:
但请注意,您可以执行以下操作:
PointerToString::PtPtS ptr2 = ptr.operator->();
run online: https://wandbox.org/permlink/Is5kPamEMUCA9nvE
在线运行:https: //wandbox.org/permlink/Is5kPamEMUCA9nvE
From Stroupstrup:
来自斯特劳斯特鲁普:
The transformation of the object pinto the pointer
p.operator->()
does not depend on the member mpointed to. That is the sense in whichoperator->()
is a unary postfix operator. However, there is no new syntax introduced, so a member name is still required after the->
对象p到指针的转换
p.operator->()
不依赖于指向的成员m。这就是operator->()
一元后缀运算符的含义。但是,没有引入新的语法,所以在后面仍然需要一个成员名->
回答by gr0v3r
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();
}
This outputs:
这输出:
Hi
回答by Anycorn
class T {
public:
const memberFunction() const;
};
// forward declaration
class DullSmartReference;
class DullSmartPointer {
private:
T *m_ptr;
public:
DullSmartPointer(T *rhs) : m_ptr(rhs) {};
DullSmartReference operator*() const {
return DullSmartReference(*m_ptr);
}
T *operator->() const {
return m_ptr;
}
};
回答by Foo Bah
The "arrow" operator can be overloaded by:
“箭头”运算符可以通过以下方式重载:
a->b
will be translated to
将被翻译成
return_type my_class::operator->()