Sunday, July 29, 2012

Yoda Conditions

Yoda-conditions
Using if (constant == variable) instead of if (variable == constant), like if (4 == foo). Because it's like saying "if blue is the sky" or "if tall is the man".

Monday, July 16, 2012

IID_PPV_ARGS

Useful macro, instead this:
hr = CoCreateInstance(
    __uuidof(FileOpenDialog), NULL, CLSCTX_ALL,
    __uuidof(IFileDialogCustomize), reinterpret_cast<void**>(&pFileOpen));

we can write:
hr = CoCreateInstance(
    __uuidof(FileOpenDialog), NULL, CLSCTX_ALL,
    IID_PPV_ARGS(&pFileOpen));

Thursday, July 12, 2012

std::vector of pointers vs vector of elements/values

Some info about pointer vs value.

Vector of elements:

+Do not need to free/allocate memory for elements
+Keep it simple
+All elements memory allocated as one memory block - it increases cache hit and reduces memory fragmentation
+STL algorithms friendly
+Do not uses additional memory for pointer

Vector of pointers:
+Polymorphism
+If we don't know how much of memory we will need
+If we will need a lot of large objects
+Small pointers size useful for pointer to elements swaps