📜 ⬆️ ⬇️

Crazy puzzles

After a heated discussion , I had a question: and, really, how many people who know (or who think they know) C ++ can answer a question similar to the “simplest test” from the article under discussion.

#include <iostream>

int test ( int x ) {
static const int y = test ( x + 1 ) * 2 ;
return x + y ;
}
')
int main ( ) {
std :: cout << test ( 42 ) << std :: endl;
std :: cout << test ( 0 ) << std :: endl ;
}

Questions:
1. For which version of GCC will this program work?
2. What will she display?
3. How to refactor the test () function to make it compatible with the standard (and with GCC 4.2, respectively)?

Well, a couple of simpler examples (approximately like problem number two there):
4. How to describe a function inside a class that does not belong to the class (that is, it is not a static or dynamic class method)?
5. Why is this feature needed in C ++?

And finally, the question is: is it worth asking such questions at the interview?

PS Tips write white on white, as usual ...

Source: https://habr.com/ru/post/19998/


All Articles