在模板类的方法中 *this = NULL 是什么意思?

发布于 2024-12-25 20:00:52 字数 396 浏览 2 评论 0原文

在模板类中,我找到了表达式 *this = NULL 这样的表达式是什么意思?

下面是它的定义:

TYPE** getPtr() 
{
 *this = NULL;
 return &m_pPtr;
}

其中m_pPtr 是模板类中的类型TYPE*​​

赋值运算符:

// Assignment operator.
TYPE* operator =(TYPE *pPtr) {
  if (pPtr == m_pPtr)
    return pPtr;

  m_pPtr = pPtr;

  return m_pPtr;
}

Vishnu。

Inside a templated class, I found the expression, *this = NULL What does such an expression mean ?

The following is its definition:

TYPE** getPtr() 
{
 *this = NULL;
 return &m_pPtr;
}

where m_pPtr is type TYPE* in the template class.

Assignment operator:

// Assignment operator.
TYPE* operator =(TYPE *pPtr) {
  if (pPtr == m_pPtr)
    return pPtr;

  m_pPtr = pPtr;

  return m_pPtr;
}

Vishnu.

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

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

发布评论

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

评论(2

呆° 2025-01-01 20:00:52

如果没有看到实际的代码,很难说出这样的声明的意义是什么。

但它可能会调用重载的赋值运算符。例如:

#include <iostream>

class X {
public:
    void operator=(void *) {
        std::cout << "Here!\n";
    }

    void foo() {
        *this = NULL;
    }
};


int main() {
    X x;
    x.foo();
}

It's difficult to say what the point of such a statement is without seeing the actual code.

But it will probably be invoking an overloaded assignment operator. e.g.:

#include <iostream>

class X {
public:
    void operator=(void *) {
        std::cout << "Here!\n";
    }

    void foo() {
        *this = NULL;
    }
};


int main() {
    X x;
    x.foo();
}
堇色安年 2025-01-01 20:00:52

它正在尝试将 0 分配给当前对象。它会调用类似的东西

operator=(void *);

另一种可能性(据我所知)是对象中有一个构造函数,它采用 void* 或类似的类型。然后它会构造一个对象,然后对其进行复制分配。

T :: T(void *);    // construct with the void *
T :: T(const T &); // copy assignment

It's attempting to assign 0 to the current object. It will call something like

operator=(void *);

Another possibility (as far as I know) is that there is a constructor in the object which takes a void* or similar type. Then it would construct an object and then copy-assign that.

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