cpp1 Flashcards
how to read variable types?
- start reading at the variable’s name
- read as far as possible until you reach the end of the declaration or
an (as yet unmatched) closing parenthesis. - return to the point where you started reading, and read backwards
until you reach the beginning of the declaration or a matching opening
parenthesis. - If you reached an opening parenthesis, continue at step 2 beyond the
parenthesis where you previously stopped.
als je een reference in een functie wil hebben naar een variable in main dan doe je:
void increase(int &valr) // expects a reference
{ // to an int
valr += 5;
}
increase(x);
how do you call a double array to a function?
Double array in parameters[][80] de 2e is belangrijk de eerste niet. Want de 2e is de grootte van hie ver de volgende pointer verder moet zijn dan de vorige.
welke casts?
static cast
const cast
reinterpret cast
what does ios do?
inherits from ios_base and additionally implements communications with a buffer used by streams.
it thus uses streambuf object
how to return mutiple things from funtcion?
use a structured binding declaration such as: struct, tuple, pair
make a function not accepting any arguments
function(void)
ifndef is an
include guard
how to prevent this backdoor?
string &Person::name() const
{
return d_name;
}
Person somebody; somebody.setName("Nemo"); somebody.name() = "Eve"; // Oops, backdoor changing the name
return a const string & ipv string &
ambiguity resolution
- More in general: when possible the compiler will interpret a syntactic construction as a declaration, rather than as a definition (of an object or variable).
- Most problems that result from the compiler interpreting constructions as declarations are caused by us using parentheses. As a rule of thumb: use curly braces, rather than parentheses when constructing objects (or values)
Type object{ value list }; Fun(fun); //making an anonymous object often doesnt work //it parses it as Fun fun which is the definition of fun using a //constructor without arguments. //however: Fun{fun} can be done
what member function is always preferred?
a const member function
how are references and pointers related
references are const pointers but no indirection takes place
To cast an object to an rvalue reference
use move semantics (e.g.
std::move(obj))
diff array and pointer
The name of an array is a pointer to its begin
location in memory.
A main difference between arrays and pointers is that an array is an rvalue, whereas a pointer is an
lvalue.
const komt
DIRECT na datgene wat const is. dus *const is een const pointer maar **const is een const pointer naar een pointer (of array).