1

Let's say whe have

class Foo{

public:
       bool   error;
       ......
       bool isValid(){return error==false;}
};

and somewhere

Foo *aFoo=NULL;

I usually would do if (aFoo!=NULL && aFoo->isValid()).....

But what if in the isValid method I test the nullity:

bool isValid(){return this!=NULL && error==false)

That would simplify the external testing with simply calling if (aFoo->isValid())

I've tested it in some compilers and it works but I wonder if it is standard and could cause problems when porting to other environments.

5
  • 1
    I don't know if it is valid, but I think that even if it is. Any way in my opinion it may be very hard to maintain a client code which assume that the class method implementation checks for nullity. Commented Sep 15, 2014 at 9:23
  • 4
    it is definitely not valid to dereference a nullptr Commented Sep 15, 2014 at 9:23
  • 1
    Im not quite sure, but I believe this cannot work as this within a class cannot be NULL, which means that the check in the method is unnecessary; on the other hand, a method call on a null object is undefined behaviour. Is that right? Commented Sep 15, 2014 at 9:23
  • 3
    I've tested it in some compilers and it works One of the worst ways to determine if a C++ construct is valid or not. Commented Sep 15, 2014 at 9:29
  • Yes @PaulMcKenzie, but that was only the firs step. Second one is checking in StackOverflow :-) Commented Sep 15, 2014 at 9:55

3 Answers 3

5

The compiler is free to optimize away the check -- calling any non-static member of any class through an invalid (or NULL pointer) is undefined behavior. Please don't do this.

Sign up to request clarification or add additional context in comments.

Comments

4

Why not simply a namespace-scope function like this?

bool isValid(Foo* f) {return f && f->isValid();}

An if-Statement like

if (aFoo->isValid())

Implies that the pointer is pointing to a valid object. It would be a huge source of confusion and very error prone.

Finally, your code would indeed invoke undefined behavior - aFoo->isValid is per definition equivalent to (*aFoo).isValid:

N3337, §5.2.5/2

The expression E1->E2 is converted to the equivalent form (*(E1)).E2;

which would dereference a null pointer to obtain a null reference, which is clearly undefined:

N3337, §8.3.2/5

[ Note: in particular, a null reference cannot exist in a well-defined program, because the only way to create such a reference would be to bind it to the “object” obtained by dereferencing a null pointer, which causes undefined behavior. […] — end note ]

Comments

4

Generally it would be bad design and in standard C++ it doesn't make much sense as your internal NULL check implies that you would call a null pointer, which is undefined behavior.

This topic was discusses here: Checking if this is null

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.