C ++重载解除引用运算符 [英] C++ overloading dereference operators
问题描述
我对C ++还是比较陌生,但仍在尝试掌握语法。我一直在看一些操作符重载的例子,最近是智能指针的实现。这是我正在查看的一个非常普通的示例:
I'm relatively new to C++, still trying to get a hang of the syntax. I've been taking a look at a few operator overloading examples, most recently smart pointer implementations. Here's a really generic example I'm looking at:
template < typename T > class SP
{
private:
T* pData; // Generic pointer to be stored
public:
SP(T* pValue) : pData(pValue)
{
}
~SP()
{
delete pData;
}
T& operator* ()
{
return *pData;
}
T* operator-> ()
{
return pData;
}
};
重载解引用运算符时,为什么类型为T& ;?类似地,当重载结构取消引用时,为什么类型T *?
When overloading the dereference operator why is the type T&? Similarly, when overloading the structure dereference why is the type T*?
推荐答案
取消引用运算符( *
)重载的工作原理与其他任何运算符重载一样。如果您希望能够修改解引用的值,则需要返回一个非常量引用。这样, * sp = value
会实际修改 sp.pData
指向的值,而不是由
The dereference operator (*
) overload works like any other operator overload. If you want to be able to modify the dereferenced value, you need to return a non-const reference. This way *sp = value
will actually modify the value pointed to by sp.pData
and not a temporary value generated by the compiler.
结构解除引用运算符(->
)重载是运算符重载的一种特殊情况。实际上会在循环中调用该运算符,直到返回实指针,然后再取消对该实指针的引用。我想这只是他们想到的实现它的唯一方法,结果发现它有点骇人听闻。但是,它具有一些有趣的属性。假设您具有以下类:
The structure dereference operator (->
) overload is a special case of operator overloading. The operator is actually invoked in a loop until a real pointer is returned, and then that real pointer is dereferenced. I guess this was just the only way they could think of to implement it and it turned out a bit hackish. It has some interesting properties, though. Suppose you had the following classes:
struct A {
int foo, bar;
};
struct B {
A a;
A *operator->() { return &a; }
};
struct C {
B b;
B operator->() { return b; }
};
struct D {
C c;
C operator->() { return c; }
};
如果您有对象 d
D
,调用 d-> bar
首先会调用 D :: operator->。 ()
,然后 C :: operator->()
,然后 B :: operator->( )
,它最终返回指向结构 A
的真实指针,其 bar
成员为以正常方式取消引用。请注意以下内容:
If you had an object d
of type D
, calling d->bar
would first call D::operator->()
, then C::operator->()
, and then B::operator->()
, which finally returns a real pointer to struct A
, and its bar
member is dereferenced in the normal manner. Note that in the following:
struct E1 {
int foo, bar;
E1 operator->() { return *this; }
};
调用 e-> bar
, e
是 E1
类型,产生无限循环。如果要实际取消引用 e.bar
,则需要执行以下操作:
Calling e->bar
, where e
is of type E1
, produces an infinite loop. If you wanted to actually dereference e.bar
, you would need to do this:
struct E2 {
int foo, bar;
E2 *operator->() { return this; }
};
总结:
To summarize:
- 重载解除引用运算符时,类型应为
T&
,因为修改指向的值是必需的通过pData
。 - 重载结构取消引用时,类型应为
T *
因为此运算符是一种特殊情况,而这正是它的工作方式。
- When overloading the dereference operator, the type should be
T&
because that is necessary to modify the value pointed to bypData
. - When overloading the structure dereference, the type should be
T*
because this operator is a special case and that is just how it works.
这篇关于C ++重载解除引用运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!