在 c++ 中重载间接运算符

发布于 2024-12-26 16:43:35 字数 583 浏览 2 评论 0原文

我的问题很简单。我有一个类模板,其中包含指向动态分配类型的指针。我想重载间接运算符,以便使用 -> 引用类模板实例。运算符我被重定向,就像我直接使用其中包含的指针一样。

template<class T>
class MyClass
 {
  T *ptr;
  ...
  // created dynamic resource for ptr in the constructor
 };

创建某种类型的 myclass:

MyClass<SomeObject> instance;

所以我想要的是不必键入:

instance.ptr->someMemberMethod();

我只需键入:

intance->someMemberMethod();

即使你 instance 不是一个指针,它的行为就好像它是指针 instance > 包含。如何通过重载操作符来弥补这一差距?

my problem is a simple one. I have a class template that holds a pointer to a dynamically allocated type. I want to overload the indirection operator so that referring to the class template instance with the -> operator I get redirected as if I use the pointer contained within directly.

template<class T>
class MyClass
 {
  T *ptr;
  ...
  // created dynamic resource for ptr in the constructor
 };

Create myclass of some type:

MyClass<SomeObject> instance;

So what I want is instead of having to type:

instance.ptr->someMemberMethod();

I simply type:

intance->someMemberMethod();

Even thou instance is not a pointer it behaves as if it is the pointer instance contains. How to bridge that gap by overloading the operator?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一身仙ぐ女味 2025-01-02 16:43:35

您可以只重载 operator->operator*

template<class T>
class MyClass
{
    T* ptr;

public:
    T* operator->() {
        return ptr;
    }

    // const version, returns a pointer-to-const instead of just a pointer to
    // enforce the idea of the logical constness of this object 
    const T* operator->() const {
        return ptr;
    }

    T& operator*() {
        return *ptr;
    }

    // const version, returns a const reference instead of just a reference
    // to enforce the idea of the logical constness of this object
    const T& operator*() const {
        return *ptr;
    }
};

请注意,由于语言创建者的设计决定,您不能重载 . 运算符。

另外,您可能认为 operator* 会重载乘法运算符,而不是取消引用运算符。然而,情况并非如此,因为乘法运算符采用单个参数(而解引用运算符不采用任何参数),因此编译器可以区分哪个是哪个。

最后,请注意 operator-> 返回一个指针,但 operator* 返回一个引用。很容易不小心将它们混淆。

You can just overload operator-> and operator*:

template<class T>
class MyClass
{
    T* ptr;

public:
    T* operator->() {
        return ptr;
    }

    // const version, returns a pointer-to-const instead of just a pointer to
    // enforce the idea of the logical constness of this object 
    const T* operator->() const {
        return ptr;
    }

    T& operator*() {
        return *ptr;
    }

    // const version, returns a const reference instead of just a reference
    // to enforce the idea of the logical constness of this object
    const T& operator*() const {
        return *ptr;
    }
};

Note that, due to a design decision by the creator of the language, you can't overload the . operator.

Also, you might think that operator* would overload the multiplication operator instead of the dereference operator. However, this is not the case, because the multiplication operator takes a single argument (while the dereference operator takes no arguments), and because of this, the compiler can tell which one is which.

Finally, note that operator-> returns a pointer but operator* returns a reference. It's easy to accidentally confuse them.

苏辞 2025-01-02 16:43:35

重载 -> 运算符:

template <typename T> class MyClass
{
    T * p;
public:
    T       * operator->()       { return p; }  // #1
    T const * operator->() const { return p; }
};

请注意,两个重载都不会改变对象;尽管如此,我们还是决定将 #1 设为非常量,以便将对象的常量性遗赠给指针对象。这有时被称为“深度常量传播”或类似的东西。语言 D 更进一步。

Overload the -> operator:

template <typename T> class MyClass
{
    T * p;
public:
    T       * operator->()       { return p; }  // #1
    T const * operator->() const { return p; }
};

Note that both overloads don't mutate the object; nonetheless we decide to make #1 non-const, so that we bequeath constness of the object onto the pointee. This is sometimes called "deep constness propagation" or something of this sort. The language D takes this much further.

2025-01-02 16:43:35

可以重载成员访问运算符以返回指向要访问的对象的指针:

T * operator->() {return ptr;}
T const * operator->() const {return ptr;}

您可能还需要引用运算符,以使其感觉更像指针;这会返回一个引用:

T & operator*() {return *ptr;}
T const & operator*() const {return *ptr;}

The member access operator can be overloaded to return a pointer to the object to be accessed:

T * operator->() {return ptr;}
T const * operator->() const {return ptr;}

You might also want the deference operator, to make it feel even more like a pointer; this returns a reference instead:

T & operator*() {return *ptr;}
T const & operator*() const {return *ptr;}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文