简体   繁体   中英

What does *this = NULL mean inside a method in a templated class?

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.

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. eg:

#include <iostream>

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

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


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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM