FactSet Research Systems, Inc Interview Question
Software Engineer / Developers#include <typeinfo>
class Tool{
public:
Tool() {};
virtual void type() = 0; // pure virtual
virtual void Action(); // virtual
private:
void init(); //private
string & Name() { return typeid(this).name();} // RTTI
};
First of all you cannot initialize non-const std::string with
const char*
which
typeid(this).name()
returns, also you are returning reference to a TEMPORARY VARIABLE!!!. If you want to return string you should return it by value.
Second I don't think name of BASE class is what they asked by saying "which class's instance this object is" not which _base_ class.
Actually, they want "Name" as a variable, not as a function.
So, the class will look like this:
class Tool
{
public:
Tool()
{
Name = typeid(*this).name();
}
// blah blah
public:
string Name;
};
Please pay attention to "typeid(*this)", not "typeid(this)", - else we will have the class name as "Tool*" instead of "Tool"!
<pre lang="" line="1" title="CodeMonkey90927" class="run-this">abstract class Tool
- Anonymous August 18, 2011{
private static Name=getClass().getName();
private final void init()
{
}
abstract public void type();
public void Action()
{
}
}
</pre><pre title="CodeMonkey90927" input="yes">
</pre>