Question 9
Back to Exercise
The program below does not compile. Which statement correctly describes the problem?
#include <iostream>
struct Plugin {
virtual const char* name() const = 0;
virtual ~Plugin() = default;
};
struct Exporter : Plugin {
const char* name() const override { return "exporter"; }
};
void show(Plugin p) { std::cout << p.name() << '\n'; }
int main() {
Exporter e;
show(e);
return 0;
}
- A name must not be const
- B An abstract class cannot be a by-value parameter type, because that would create a Plugin object; take const Plugin& instead Correct
- C Exporter must be final
- D show must be virtual
Correct Answer: Option B
Passing by reference also avoids slicing, which is exactly what you want for polymorphic objects.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!