-1

I was recently asked this question in an interview:

#include <iostream> class Base { public: void foo() { std::cout << "foo" << std::endl; } }; class Derived : public Base { public: void bar() { std::cout << "bar" << std::endl; } }; int main(int argc, const char *argv[]) { Base *p = new Derived; // additional code here return 0; } 

The conditions on the question were that the Base and Derived classes cannot be changed (for example changing the name of the methods, adding additional methods, or changing a method to virtual. A further restriction was that no type of cast could be used. The pointer p had to be used. Other than that, you could write any additional code, including as many classes as necessary to insure that the "bar()" method was called using the object pointed to by p.

Given that no casts were allowed, the only aswer I could come up with was an old-school one:

Derived *d; memcpy(&d, &p, sizeof p); d->bar(); 

Which is even worse than a cast. The interviewer berated me and told me I didn't have even the most basic knowledge of object hierarchy since I could not see the very obvious, trivial solution to the question.

I apologize if this question is a duplicate; I've seen other questions about accessing a method in a derived class from a base class, but in all cases I saw, the answer involved either a cast or modification to either of the classes.

He may be correct; I've been programming in C++ for over 15 years and I cannot see the solution. It could be I've never encountered it since I would use a cast in this situation: in this case, it would have to be a static_cast since there are no virtual methods (not even the destructor) which would allow the dynamic_vast to compile (it fails with a message: "'Base' is not a polymorphic type"

16
  • 7
    Please fix the code so that it compiles and makes sense (e.g. what is the relationship between Base and Derived?) Commented Feb 20, 2015 at 15:30
  • 1
    maybe the point was to make Derived derive from Base? Commented Feb 20, 2015 at 15:35
  • 1
    Is there any reason you don't want to fix the code? Commented Feb 20, 2015 at 15:39
  • 2
    Have you actually asked said interviewer what his solution is? Commented Feb 20, 2015 at 15:41
  • 2
    Sorry -- I fixed the inheritance Commented Feb 20, 2015 at 16:00

5 Answers 5

6

Simple and easy dumb:

#define Base Derived 

just before main. (you can then call bar on it)

Sign up to request clarification or add additional context in comments.

8 Comments

All I have to say is wow. I would hate that interview question.
Still wouldn't compile ;-)
@chris answer it, then get up and say "if this is the way you do things, I probably wouldn't fit here" :)
@LuchianGrigore, If I wasn't a desperate co-op student maybe :) I could probably stand four months.
I don't think that is the answer that demonstrates "simple, obvious" knowledge of C++ inheritance.
|
2

I maybe would come up with something like:

void foobar(Base* b){ Derived d; d.bar(); } int main(int argc, const char *argv[]){ Base *p = new Derived; foobar(p); return 0; } 

And if the interviewer complains that this is too foobar, I would ask him to please ask less foobar questions :P

No, really, I do consider this as a valid answer to a quite academic question. I am using the object pointed by p (to call a foobar function) and I made sure that 'bar()' is called. I dont think such an exercise deserves a more sophisticated solution. Logically, my solution cannot be distinguished from whatever solution the interviewer had in mind.

1 Comment

Well, I think this question really actually asks for a "worst abuse of the rules"-answer, so +1. IOCCC greets you, this time in C++.
1

The pointer p had to be used. Other than that, you could write any additional code, including as many classes as necessary to insure that the "bar()" method was called using the object pointed to by p.

As many classes as necessary, you say?

#include <iostream> class Base { public: void foo() { std::cout << "foo" << std::endl; } }; class Derived : public Base { public: void bar() { std::cout << "bar" << std::endl; } }; int main() { class Base { public: void bar() { std::cout << "bar" << std::endl; } }; class Derived : public Base { }; Base *p = new Derived; p->bar(); } 

9 Comments

Well, that's cheating. Because you don't actually use the pointer anymore, you made it into something else. Although this interviewer may have expected this answer. I guess we'll never know.
@ChristianHackl The interviewer definitely didn't expect the #define Base Derived answer.
Yeah, that answer suffers from the same problem, of course. Perhaps the interviewer did expect it, perhaps he didn't. It's just a very stupid question, and I feel sorry for the OP who wasted his time trying to get into a company like that.
"The conditions on the question were that the Base and Derived classes cannot be changed" - class Derived : public Base definitely changes the classes.
@Xaq: Or he wanted to test your reaction to the question to see how you'd cope with a superior's weak technology competence. Which is still very poor interview style. As I said, be glad you don't have to work there and find a better company, one which does not play games with candidates.
|
0

You could use a union for type-punning:

union { Base* bp; Derived* dp; } my_union = {p}; my_union.dp->bar(); 

3 Comments

I could, but that basically performs the same operation as the memcpy -- not what he was looking for
So what was he looking for?
@trenki He refused to give me the answer, only stating that I could created as many new classes as necessary to solve the problem. I also implement a MyDerived class that was public Derived and took a Base * as a parameter to the constructor and that used placement new to use it, but he didn't like that either. He kept saying that it was simple and I needed to implement some additional classes (not in main)
-1

Answer could be: This will not compile.

1 Comment

I forgot to indicate Derived is public Base. I've fixed it now.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.