home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 32,994 of 33,346   
   Gerhard Fiedler to All   
   Re: What does null mean?   
   22 Apr 13 21:31:07   
   
   From: gelists@googlemail.com   
      
   On 2013-04-22 16:50:28, in comp.lang.c++.moderated DeMarcus wrote:   
      
   >>> If you read this code, what do you think it does?   
   >>>   
   >>> CameraMan cm( nullptr );   
   >>>   
   >>> It could mean anything, right?   
   >>>   
   >>> [...]   
   >>>   
   >>> My question is; would you all agree that nullptr is a magic number   
   >>> that always should be replaced with a label like the following? const   
   >>> std::nullptr_t BAD_ANGLE = nullptr;   
   >>   
   >> For one thing, nullptr has the general meaning of "pointee not   
   >> existent". In cases where it has this meaning, there's no need to define   
   >> a named constant.   
   >>   
   >   
   > I agree on that. There's no need to make it more complicated. I would   
   > still initialize variables like this.   
   >   
   > int* anglep = nullptr;   
   >   
   >> In cases where it doesn't have this meaning, I have the suspicion that   
   >> maybe one shouldn't use nullptr in the first place.   
   >   
   > Maybe my BAD_ANGLE was a bit misleading, a better label would have been   
   > ANGLE_MISSING. Your "pointee not existent" is correct information but   
   > quite often not complete enough. I would like to be able to say "angle   
   > pointee not existent".   
   >   
   > Usually the problem shows up at function calls like:   
   > CameraMan cm( nullptr );   
   > cm.prepare( 17, "r", nullptr );   
   >   
   > So maybe the problem isn't with the labeling but with the naming of the   
   > functions. In above example it could be:   
   > cm.insertReelNumber( 17 );   
   > cm.setReadOnly();   
   > cm.setAngle( nullptr );   
   >   
   > But when you really must have a pointer in the constructor it's much   
   > easier and explicit to read:   
   > CameraMan cm( ANGLE_MISSING );   
      
   One possibly better choice here would be to provide a default ctor for   
   the cases where the angle is missing, like   
      
   CameraMan cm;   
      
   Of course this requires that the missing angle is not a variable that   
   may have nullptr as value, but this is not part of the use cases you   
   describe.   
      
   Or provide a function that decides what to do in such a case   
      
   CameraMan cm( GetDefaultAngle() );   
      
      
   My general point is that in cases where the meaning of nullptr when used   
   as that isn't obvious, chances are that it's being abused as a special   
   value -- and I don't like special values in general, not just with   
   pointers. (For example, imagine an interface where an integer argument   
   can have meaningful positive values and the negative values -1, -2 and   
   -3 are 'overloaded' to trigger special behavior. Shudder.)   
      
   Gerhard   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca