class widget { ... };How many ways (exclusing templates) can the definition of the class fubar depend on widget?
I can think of thirteen ways:
class fubar : public widget // 1 inheritance { void value_parameter(widget ); // 2 void ref_parameter(widget &); // 3 void ptr_parameter(widget *); // 4 widget value_return(); // 5 widget & ref_return(); // 6 widget * ptr_return(); // 7 widget instance_value_member; // 8 widget & instance_ref_member; // 9 widget * instance_ptr_member; // 10 static widget static_value_member; // 11 static widget & static_ref_member; // 12 static widget * static_ptr_member; // 13 };Now for the test.
Which of the thirteen ways require a hash include
#include "widget.hpp"as opposed to a forward declaration
class widget;I've given this test to many many programmers and the vast majority get it wrong. The variety of answers I get is amazing. I recently gave it to a room of about 100 C++ developers and 5 got it right with about 20 different wrong answers (at which point I stopped counting).
What's your answer?
Once you've decided you can scroll down to find the answer.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.keep scrolling...
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
The answer is 1 and 8.
How did you do?
If you don't believe me why not try it now in cyber-dojo (click the link and then click the start button).
Just so anyone can check why: http://stackoverflow.com/questions/553682/when-to-use-forward-declaration
ReplyDelete