A function can return a value of type T in one of several different ways. Which of these is correct? If correct, give a short declaration of a function that returns its value by these mechanism. Comment on the const methods of function return value. Comment on when there is danger involved in returning a reference of any kind from a function. Finally, which one of these returns an l-value and which an r-value?
a. By value
b. By lazy evaluation.
c. By reference
d. By const value
e. By const reference
a) T f() x
b) Not a mechanism known in C++
c) T& f()
d) const T f()
e)const T& f()
Explanation: d) and e) return a const value and reference respectively. This means you cannot call a mutator on the returned value, as f( ).mutator( ).Returning a reference to an object with a short life (a local variable is the typical error) will seem to work but fail horribly at some time later. c), returning a reference provides and l-value, that may be also be used as an r-value. Parts a) d) and e) return an r-value only, though a mutator can be called on a)’s return value. Finally a) and d) call the copy constructor to effect the return of a class object.
You might also like to view...
To tile multiple open worksheets on the screen, use the ________ command
A) Rearrange B) Arrange All C) Split D) Go To
What are the advantages and disadvantages of the Use Previews check box when exporting media?
What will be an ideal response?