How bad is “if (!this)” in a C++ member function?

前端 未结 11 673
不思量自难忘°
不思量自难忘° 2021-02-04 23:16

If I come across old code that does if (!this) return; in an app, how severe a risk is this? Is it a dangerous ticking time bomb that requires an immediate app-wide

11条回答
  •  被撕碎了的回忆
    2021-02-04 23:47

    In this case I'd suggest removing the NULL check from the member function and create a non-member function

    CThingy* SafeLookup(CLookupThing *lookupThing) {
      if (lookupThing == NULL) {
        return NULL;
      } else {
        return lookupThing->Lookup();
      }
    }
    

    Then it should be easy enough to find every call to the Lookup member function and replace it with the safe non-member function.

提交回复
热议问题