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

Tuesday, February 28, 2012

class and struct in C++

Differences between keywords class and struct in C++:
- In absence of an access-specifier for a base class, public is assumed when the derived class is declared struct and private is assumed when the class is declared class;
- Member of a class defined with the keyword class are private by default. Members of a class defined with the keywords struct or union are public by default;
- Keyword class can be used to declare a template parameter, while struct cannot;
- Keyword struct is compatible with C language, while class is not
.