Bloomberg LP Interview Question
Software Engineer / Developersthe question is wrong...there has to be 'operator<<' which is overloaded here...
I think this should work...
char* operator<<(ostream &o,Person &p){return p.GetName();}
friend std::ostream& operator<< (std::ostream& o, Person const& p){ return o << p-> m_szName};
E is correct answer.
Here is complete program.
class Person{
public:
Person(const char* szName)
{
m_szName = new char();
strcpy_s(m_szName,strlen(szName)+1, szName);
}
const char* GetName() const
{
return m_szName;
}
operator const char*() const { return GetName(); };
private:
char *m_szName;
};
int main(int argc, char **argv)
{
Person person("John");
std::cout <<person;
getchar();
}
output
It will print "John"
E. Becoz GetName() returns const char* . So compiler should try to type cast the person object in const char* .
- Amit March 18, 2010