如何在不知道成员名称的情况下列出班级成员 [英] how do list out the members of the class without knowing a member name directly

查看:84
本文介绍了如何在不知道成员名称的情况下列出班级成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨编码员,

在采访中我问了一个用c ++编写代码的问题,l b 该类的所有成员函数。





谢谢,

Bijay

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

推荐答案

反思是 C ++ 不支持。你必须编写一个解析器才能完成这样的任务。
Reflection is not supported by C++. You have to write a parser in order to accomplish such a task.


请参阅解决方案1.



一个有趣的产品这种类型可以在Qt中找到: http://vasc.ri.cmu。 edu / media / manuals / qt.2.3.1 / moc.html [ ^ ]。



参见http://woboq.com/blog/reflection-in-cpp-and-qt-moc.html [ ^ ]。



-SA
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


如果该问题应该快速回答,我的回答将是解决方案1中所述的内容。



如果问题是捕获粗略描述该怎么做,答案就是编写一个C ++解析器。也就是说,使用C ++执行此类任务效率非常低,因为C ++是解析和操作字符串的最差语言之一。



但是,由于这是一个面试问题,让我们假设这是一个纯粹的假设情景,不受任何常识*的影响,你确实应该这样做编写一个C ++程序来完成这项任务。



在这种情况下,您需要编写的第一件事是一个成熟的预处理器,它将C ++代码转换为定义类和代码的实际C ++语法 - 那里没有先处理所有宏和预处理器命令,无法确定任何类方法!由于不同的编译器提供不同的预处理器命令,并且编译器开关(例如-d _DEBUG)可能导致生成完全不同的代码,这一点很复杂!



下一步需要一个首先定位类定义的解析器,然后是单个成员函数。由于模板语法的原因,这可能相当棘手,当模板特化分布在多个文件上时,它很容易变得非常复杂。基本上你需要一个功能齐全的C ++模板解析器来查找和解决所有模板实例化!



此时你可能想再次询问面试官,如果这是他真的想要你做什么,以及他是否忘记提及他希望你处理的类定义的一些限制因素。或者,他是否会考虑允许你对编写程序(你自己,一个人,没有其他来源的帮助)条件留有一些余地。因为,上面列出的任务只需要一位经验丰富的程序员,即使不是几个月就可以完成,除非他当然可以选择现有的库,这些库可以开箱即用,也许只是稍加调整......



当然,如果这是一个真实世界的任务,我要做的第一件事就是挑战隐含的要求和限制,而不是预期目标的直接后果(写一个类函数列表)。另外,我会问这个列表的目的是什么。例如,如果该类列表用于文档,那么很容易找到已经存在的特定解决方案,或者至少是工具(例如doxygen),这些工具不仅可以简化任务,而且还可以提供更好的结果! />


简而言之,开发软件中只有一小部分是编写代码,最大的部分是找出真正的规范。





当然,根据你采访的位置,我的回复可能会让面试官认为你的资格过高(已经发生在我身上) - 也许是他的全部观点问题是将黑客与建筑师分开,如果他需要的只是一些廉价的黑客,那么你最好只是简单地写下一些可能只是在一个微不足道的情况下工作的代码。(br) />




*:当我想到这一点时,很少有真实世界的项目在所有方面都显示出常识,所以也许它不是*那个假设毕竟...
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...


这篇关于如何在不知道成员名称的情况下列出班级成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆