Checking for a null object in C++

后端 未结 8 1072
南旧
南旧 2020-12-13 17:55

I\'ve mostly only worked with C and am running into some unfamiliar issues in C++.

Let\'s say that I have some function like this in C, which would be very typical:<

相关标签:
8条回答
  • 2020-12-13 18:04

    C++ references naturally can't be null, you don't need the check. The function can only be called by passing a reference to an existing object.

    0 讨论(0)
  • 2020-12-13 18:14

    A reference can not be NULL. The interface makes you pass a real object into the function.

    So there is no need to test for NULL. This is one of the reasons that references were introduced into C++.

    Note you can still write a function that takes a pointer. In this situation you still need to test for NULL. If the value is NULL then you return early just like in C. Note: You should not be using exceptions when a pointer is NULL. If a parameter should never be NULL then you create an interface that uses a reference.

    0 讨论(0)
  • 2020-12-13 18:14

    As everyone said, references can't be null. That is because, a reference refers to an object. In your code:

    // this compiles, but doesn't work, and does this even mean anything?
    if (&str == NULL)
    

    you are taking the address of the object str. By definition, str exists, so it has an address. So, it cannot be NULL. So, syntactically, the above is correct, but logically, the if condition is always going to be false.

    About your questions: it depends upon what you want to do. Do you want the function to be able to modify the argument? If yes, pass a reference. If not, don't (or pass reference to const). See this C++ FAQ for some good details.

    In general, in C++, passing by reference is preferred by most people over passing a pointer. One of the reasons is exactly what you discovered: a reference can't be NULL, thus avoiding you the headache of checking for it in the function.

    0 讨论(0)
  • 2020-12-13 18:19

    Basically, all I'm trying to do is to prevent the program from crashing when some_cpp_function() is called with NULL.

    It is not possible to call the function with NULL. One of the purpose of having the reference, it will point to some object always as you have to initialize it when defining it. Do not think reference as a fancy pointer, think of it as an alias name for the object itself. Then this type of confusion will not arise.

    0 讨论(0)
  • 2020-12-13 18:20

    A C++ reference is not a pointer nor a Java/C# style reference and cannot be NULL. They behave as if they were an alias to another existing object.

    In some cases, if there are bugs in your code, you might get a reference into an already dead or non-existent object, but the best thing you can do is hope that the program dies soon enough to be able to debug what happened and why your program got corrupted.

    That is, I have seen code checking for 'null references' doing something like: if ( &reference == 0 ), but the standard is clear that there cannot be null references in a well-formed program. If a reference is bound to a null object the program is ill-formed and should be corrected. If you need optional values, use pointers (or some higher level construct like boost::optional), not references.

    0 讨论(0)
  • 2020-12-13 18:21

    You should use NULL only with pointers. Your function accepts a reference and they can't be NULL.

    Write your function just like you would write it in C.

    0 讨论(0)
提交回复
热议问题