-2

This a C++ online test question. The test has been done.

class Person
{
    std::string name;
    public:
          std::string const &getname(void) const ;      
} ;

inline std::string const &Person::getname() const
{
        return name; 
}

A: The computer inserts the code of the function getname()

B: The computer generates a call to the function getname()

C: The default parameter values of the calling function are returned

D: All arguments of the function are placed on the memory stack

I choose A. Is it correct ?

Thanks.

user1002288
  • 4,860
  • 10
  • 50
  • 78
  • 4
    I think you accidentally the question. Also, 3 of the 4 choices are labelled "A". Which "A" did you choose? – André Caron Feb 24 '12 at 19:42
  • 2
    About A: The compiler *might* make the function inline. – jrok Feb 24 '12 at 19:44
  • @AndréCaron I'm assuming he meant the first since the other two values are very likely a typo. Unfortunately, in this case I think it comes down to what your particular compiler wants to do so like all great engineering answers "It depends..." –  Feb 24 '12 at 19:45
  • 1
    I think the answer is "E: `b` and `v`.", but I'm assuming the question was "Which characters appear only once in the following code?", so I could be wrong. It could also be "F: the compiler complains about a missing semicolon before `inline`." if the question was "What will a C++ compiler do with this code?". – R. Martinho Fernandes Feb 24 '12 at 19:48
  • possible duplicate of [Benefits of inline functions in C++?](http://stackoverflow.com/questions/145838/benefits-of-inline-functions-in-c) – Mooing Duck Feb 24 '12 at 19:54

2 Answers2

2

The compiler may inline the function.

The compiler/linker will not complain when the function body is found in multiple compilation units, as long as the body is the same.

1

From the C++11 draft

§ 7.1.2\2

A function declaration (8.3.5, 9.3, 11.3) with an inline specifier declares an inline function. The inline specifier indicates to the implementation that inline substitution of the function body at the point of call is to be preferred to the usual function call mechanism. An implementation is not required to perform this inline substitution at the point of call; however, even if this inline substitution is omitted, the other rules for inline functions defined by 7.1.2 shall still be respected.

So, as for requirements, it's basically just a normal function, so option B. It might or might not have it's code inserted into the calling function, but that also applies to functions not marked inline, and thus isn't really anything to do with the inline keyword.

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158