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.

Computer Science & Information Technology

You might also like to view...

CRC stands for __________.

a. Class, Recyclability, Collaborations b. Class, Redundancy, Collections c. Class, Responsibilities, Collaborations d. Code, Reuse, Constancy

Computer Science & Information Technology

A ________ is software that controls access to data in a database.

a) database management server b) structured query language c) database management system d) data manipulation language

Computer Science & Information Technology

Write an SQL statement that returns the names (not the Ids) of all students who received an A in CS305 in the fall of 2000.

What will be an ideal response?

Computer Science & Information Technology

Which of the following is a positioning type?

A. default B. relative C. justified D. centering

Computer Science & Information Technology