Some C++ References

C++ Operators

The following gives you the level of all C++ operators. It is often useful to know.

Level Operator Description Grouping
1 :: scope Left-to-right
2 () [] . -> ++ -- dynamic_cast static_cast reinterpret_cast const_cast typeid postfix Left-to-right
3 ++ -- ~ ! sizeof new delete unary (prefix) Right-to-left
* & indirection and reference (pointers)
+ - unary sign operator
4 (type) type casting Right-to-left
5 .* ->* pointer-to-member Left-to-right
6 * / % multiplicative Left-to-right
7 + - additive Left-to-right
8 << >> shift Left-to-right
9 < > <= >= relational Left-to-right
10 == != equality Left-to-right
11 & bitwise AND Left-to-right
12 ^ bitwise XOR Left-to-right
13 | bitwise OR Left-to-right
14 && logical AND Left-to-right
15 || logical OR Left-to-right
16 ?: conditional Right-to-left
17 = *= /= %= += -= >>= <<= &= ^= |= assignment Right-to-left
18 , comma Left-to-right

Note: post increment and decrement uses a dummy int parameter: T::operator ++ (int) for t++;.

The min and max operators, which were depracated around version 4.0 of gcc/g++ had the same priority as the relational operators (so priority of 9 in my table above.)

C++ Escape Sequences

The following are the standard and not so standard C/C++ escape characters supported in strings.

Sequence Character Name
\a 0x07 / 7 Bell or alert
\b 0x08 / 8 Backspace
\f 0x0C / 12 Formfeed
\n 0x0A / 10 Newline (next line)
\r 0x0D / 13 Carriage Return (beginning of line)
\t 0x09 / 9 Horizontal Tab
\v 0x0B / 11 Vertical Tab
\' 0x27 / 39 Single Quote
\" 0x22 / 34 Double Quote
\\ 0x5C / 92 Backslash
\? 0x3F / 63 Literal Question Mark
\0 0x00 / 0 Null
\OOO 0x## / # Octal Notation Character
\xHH 0x## / # Hexadecimal Notation Character (char)
\xHHHH 0x#### / # Hexadecimal Notation Character (wchar_t)

In general, for a character c, \c will represent c if not otherwise defined in this table.

A backslah followed by a newline is used to continue a line in preprocessor code.

Use RAII with structures allocated by the C library

Once in a while, we make use of a function found in the C library (and at times even other libraries) which return a pointer to an object that needs to be released one we are done with it.

The easiest way to do that with C++ and especially RAII (in case you may get an exception before you reach the line were you would otherwise free the resource) you can use a shared pointer in this way (example for using getifaddrs() as found in snapcommunicator.cpp at this point):

namespace
{

void ifaddrs_deleter(struct ifaddrs * ptr)
{
    freeifaddrs(ptr);
}

} // no name namespace

...
  {
    struct ifaddrs * if_ptr;
    if(getifaddrs(&if_ptr) == 0)
    {
      std::shared_ptr<struct ifaddrs> safe_if_ptr(if_ptr, ifaddrs_deleter);

      ... // do work here, if exception or return, if_ptr gets freed
    }
    // else -- handle error case
  }
...

You may use a lambda function, just know that there are issue in doing so. I find it easier to have an explicit deleter like here. It is pretty nice and simple to use.

Snap! Websites
An Open Source CMS System in C++

Contact Us Directly