C++ Basics Flashcards
What is
int a = 5;
called
Copy assignment
Default initialization
int x;
Not advised as in most cases, leaves variable with indeterminate value
Copy initialization
int x = 5;
Not advised as doesn’t do any validation
Direct initialization
int x (5);
Not advised as doesn’t do any validation
Direct list initialization
int x {5};
Preferred as disallows narrowing conversions
Copy list initialization
int x = {5};
Preferred as disallows narrowing conversions
Value initialization
int x{};
Usually performs zero initialisation
What is preferred assignment or initialisation and why?
initialisation is preferred because it means variables won’t have indeterminate values if we access them before assignment which could result in undefined behaviour
std::endl
outputs a newline character and flushes any pending output to the console
‘\n’
outputs a newline character and lets system decide when to flush pending output to the console