Odd things about C/C++

Sunday, February 8th, 2004
  • There is no syntax for describing a number in binary notation. You can express a number in decimal [65], hexadecimal [0×41], octal [0101] or as a character-constant [’A'], but there is no binary equivalent [eg 0b1000001]. Seriously, who needs octal more than binary?
  • The character-constant notation [’A'] can contain more than 1 character, thus allowing a large base-256 number to be described (as long as characters used are legal within the c-char sequence). eg: the character sequence ‘AB’ equates to the decimal value 16706 [’A’ * 256 + ‘B’]. Although it is of course very bad programming practise, this allows a sort of middle ground between evil magic numbers and annoying enums. Eg: you could return integer codes which are actually human readable to a degree (as long as they have no more than 4 characters) Examples: ‘ok’, ‘err’, ’stop’, ‘play’ etc
  • When operator overloading is used on postfix operators [eg: T++] a dummy parameter is used to differentiate between postfix and prefix. This seems a bit like a quick hack that just got left in. So to override ++T we use T operator++(), whereas to override T++ we use T operator++(int), with an integer parameter that isn’t actually there. What’s really annoying about this syntax is that it implies a binary operator++ [perhaps the original implementation did in fact create this dummy int, and the postfix operator is actually a binary operator with an implied parameter]
feed

Leave a Comment