c++ - Boolean value returned on a function call to null object -
what boolean value returned when fucntion call made on null object in c++?
classdummy* dummy = null; if (!dummy->dummy_function(1,2,3)) { // }
shouldn't return error according c++11 standards?
unless dummy
has been declared @ namespace scope, uninitialized , value unspecified, i.e. may or may not null. invoking member function on nullptr
, or on pointer pointing invalid memory, undefined behavior.
you might get away correct result if member function invoke doesn't access other data members of class; in other words, if doesn't dereference this
pointer. however, regardless of whether obtain expected result or not, still undefined behavior.
compilers not required detect such invalid function calls. if obvious, initialize object pointer nullptr
, invoke member function on it, may see diagnostic compiler, not guaranteed.
Comments
Post a Comment