Why is there a performance warning on cast pointer to bool?

前端 未结 6 1947
花落未央
花落未央 2020-12-09 01:14

Extends.

I thought I was being cool when I did something like:

bool hasParent()
{
  return this->parentNode ;
}

Even with a (bool) cast, the

6条回答
  •  孤城傲影
    2020-12-09 01:44

    The fact that casting to bool does not make the warning go away is by design:

    Casting the expression to type bool will not disable the warning, which is by design.

    I would recommend the approach that the MSDN description of warning C4800 recommends:

    return this->parentNode != NULL;
    

    this makes it clear that you are returning true if parentNode is not a null pointer and false if parentNode is a null pointer.

提交回复
热议问题