Accelerated C++ Solution to Exercise 4-8

Exercise 4-8

If the following code is legal, what can we infer about the return type of f?

double d = f() [n]

Solution

The return type of the function f is of type double. The logic is a follows:

  • The statement is trying to define a variable double d. i.e. a variable called d of type double.
  • The equal (=) sign assigns whatever that is on the right (of the equal sign), to the left (of the equal sign).
  • As we know that the left-hand-side is of type double, it must be true that the right must also be of type double.
  • Hence, the return type of the function f, is indeed of type double.

Reference

Koenig, Andrew & Moo, Barbara E., Accelerated C++, Addison-Wesley, 2000

2 thoughts on “Accelerated C++ Solution to Exercise 4-8”

  1. I think you’ve not got this one correct. f()[n] means that f() must return a type that allows access via [n], i.e. a vector of doubles, or array of doubles or something. Thanks for putting all these up by the way, really useful for checking answers against when you don’t understand something! Hope you don’t mind me suggesting corrections.

    1. Thanks for pointing this. I must admit I did not have a clue of what the question was really all about so I just did my best :) And glad that the posts have helped you! The initial motivation of posting these were purely to keep a record so I could refer back when/if I forget things (without expecting people to even find these on Google). So I am actually very glad that these posts actually have helped somebody else!

Leave a reply