Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Coders,
In interview i have asked a question write a code in c++ which will list out all the member functions of that class.


Thanks,
Bijay
Posted
Comments
[no name] 29-Oct-15 6:45am    
If they put you in a locked room and gave you only access to Google what would you have done.
jeron1 29-Oct-15 9:56am    
Looked up Pr0n?

Reflection is not supported by C++. You have to write a parser in order to accomplish such a task.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 29-Oct-15 11:24am    
That's right a 5.
I referenced one such product in Solution 2, please see, pretty interesting.
—SA
CPallini 29-Oct-15 17:10pm    
Thank you.
Please see Solution 1.

One interesting product of this kind can be found in Qt: http://vasc.ri.cmu.edu/media/manuals/qt.2.3.1/moc.html[^].

See also http://woboq.com/blog/reflection-in-cpp-and-qt-moc.html[^].

—SA
 
Share this answer
 
Comments
CPallini 29-Oct-15 17:10pm    
5.Really nice stuff.
Sergey Alexandrovich Kryukov 29-Oct-15 17:15pm    
Thank you, Carlo.
—SA
If that question was supposed to be answered quickly, my response would be what is stated in solution 1.

If the question was fishing for a rough description of what to do, the answer would be to write a C++ parser. That said, using C++ for such a task would be very inefficient, as C++ is one of the worst languages for parsing and manipulating strings.

However, since this is an interview question, let's assume this is a purely hypothetical scenario that is not in any way influenced by common sense*, and that indeed you should write a C++ program to accomplish this task.

In that case, the first thing you need to write is a full-fledged preprocessor that transforms the C++ code into the actual C++ syntax defining the classes and code - there is no sure way to identify any class methods without first processing all macros and preprocessor commands! This bit is complicated by the fact that different compilers offer different preprocessor commands, and that compiler switches (e. g. "-d _DEBUG") can lead to entirely different code being generated!

The next step requires a parser that locates first the class definition, and then the individual member functions within. This can be rather tricky due to template syntax, and it can easily become very complex when there are template specializations spread over multiple files. Basically you need a fully functional C++ template parser to find and resolve all template instantiations!

At this point you might want to ask the interviewer (again), if this is really what he wants you to do, and whether he may have forgotten to mention some limiting factors about the class definitions he wants you to process. Or, whether he would consider allowing you some leeway regarding the 'write a program (yourself, all alone, without help from other sources) condition. Because, just the tasks outlined above will take an experienced programmer many weeks if not months to complete, unless of course he's allowed to just pick up already existing libraries that can do this out of the box, maybe just with a little tweaking...

Of course, if this were a real world assignment, the first thing I would do is challenge the implied requirements and restrictions that are not direct consequences of the intended goal (write a list of class functions). Additionally, I would ask for the purpose that this list is intended for. For instance if that class list is meant for documentation, it's easy enough to find specific solutions that are already in existance, or at least tools (such as doxygen) that not only simplify the task cconsiderably, but also offer much better results!

In short, only a small part of developing software is about writing code, the biggest part is about finding out the real specification.


Of course, depending on the position you interviewed for, a response such as mine might get the interviewer to rate you as overqualified (already happened to me) - maybe his whole point of the question was to separate hackers from architects, and if all he needs are a few cheap hackers, you'd be better off to just pen down a few (naive) lines of code that could just work in a trivial case.


*: when I think about it, few real world projects show common sense in all aspects, so maybe it's not *that* hypothetical after all...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900