Checking for a null object in C++

后端 未结 8 1074
南旧
南旧 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: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.

提交回复
热议问题