Functions Flashcards
Chapter 6
What will be the argument order of evaluation for
fact( 10*10, 20+20, 30/20)
There are no guarantees about evaluation oder of parameters
When is a local static object in a function initialized and when is it destroyed
It is initialized when first time execution passes through the object’s definition and is destroyed when program terminates
Can we redefine void fcn(const int i){} as void fcn(int){}
No, as top level const-ness is ignored in a parameter
int i = 42; const int *cp = &i; // 1 const int &r = i; // 2 const int &r2 = 42; // 3 int *p = cp; // 4 int &r3 = r; // 5 int &r4 = 42; // 6
- OK, cp cant change value of i
- OK, r can’t change value of i
- OK, const reference can hold literal values
- ERROR, can’t ignore top const
- ERROR, can’t ignore top level const
- ERROR, non-const reference can hold literals values
void reset (int &i);
int i = 0;
const int ci = i;
std::string::size_type ctr = 0;
reset(&i); // 1 reset(&ci); // 2 reset(i); // 3 reset(ci); // 4 reset(42); // 5 reset(ctr); // 6
- calls the version of reset that has an int* parameter
- error: can’t initialize an int* from a pointer to a const int object
- calls the version of reset that has an int& parameter
- error: can’t bind a plain reference to the const object ci
- error: can’t bind a plain reference to a literal
- error: types don’t match; ctr has an unsigned type
What is the difference between the three void print(const int*); void print(const int[]); void print(const int[10]);
All declarations are equivalent: Each declares a function with a single parameter of type const int. When the compiler checks call to print, it checks only that the argument has type const int:
3 Common techniques to pass an array to a function
- Using a marker to specify extent an array ( eg null )
- Pass the range of first and last pointers ( printarr(begin(), end()); )
- Explicitly passing a size parameter ( printarr(arr, 10);)
How to delare a function which takes reference to an array as parameter
void print(int (&arr)[10]); () is mandatory as &arr[10] will declare array of references
How to delare a function which takes multi dimensional array as parameter
void print(int (*matrix)[10] With any array, a multidimensional array is passed as a pointer to its first element. Because we are dealing with an array of arrays, that element is an array, so the pointer is a pointer to an array. The size of the second (and any subsequent) dimension is part of the element type and must be specified. Parentheses around matrix is necessary
How to use initializer_list
void print(std::initializer_list li);
print ({1, 2, 3});
Why should we not return reference to a local object
// disaster: this function returns a reference to a local object
const string &manip()
{
string ret;
// transform ret in some way
if (!ret.empty())
return ret; // WRONG: returning a reference to a local object!
else
return “Empty”; // WRONG: “Empty” is a local temporary string
}
Both of these return statements return an undefined value—what happens if we try to use the value returned from manip is undefined. In the first return, it should be
obvious that the function returns a reference to a local object. In the second case, the string literal is converted to a local temporary string object. That object, like the
string named s, is local to manip. The storage in which the temp
What is the associativity of function call operator
Its left assiciative
hence following is possible
auto lenght = std::string(“hello”).size();
How to return pointer to an array
1. typedef/ type aliasing tyepdef int arr[10]; using arr = int[10]; arr* func(int i); 2. using the form Type (*function(parameter_list))[dimension] int (*func(int))[10]; 3. Trailing return type auto func(int) -> int(*)[]; 4. using decltype int odd = {1, 2, 3, 4}; decltype (odd) *func(int);
Can we overload function differing on reference const-ness
Yes, int lookup(int &i); int lookup(const int &i);
Similarly following can be overloaded int lookup(int *i); int lookup(const int *i);
Does a local function definition ( or declaration ) hide other function declarations?
Yes,
void print(double); void print(std::string); void foo() { void print(); print(3.14); // ERROR, this is hidden print("Hello"); // ERROR, this is also hidden print();// OK, this is available }