Reputation: 16422
I have a struct that I use a pure abstract interface (only public methods and they're all =0
), an abstract class to hide implementation details and two child classes that inherit from it.
I'd like to expose some public methods in those child classes as they only make sense in that context but marking them as public doesn't work as the compiler only seems to be able to see public methods in the interface. How can I make public methods in the child classes accessible?
Update
Interface:
class Result {
public:
virtual ~Result() noexcept = default;
protected:
Result() = default;
};
Abstract class:
template <typename T>
class AbstractResult : public Result {
public:
AbstractResult();
virtual ~AbstractResult() noexcept = default;
};
First child:
class AResult : public AbstractResult<PGResult> {
public:
PGResult() = default;
virtual ~PGResult() noexcept = default;
void add_server_status(const char status) noexcept;
void add_command_complete(const CommandComplete command_complete) noexcept;
void add_columns(const vector<Column> columns) noexcept;
void add_error(const Error error) noexcept;
void add_notification(const Notification notification) noexcept;
};
I'd like to create an instance of Result
and call add_columns(...)
on it which is forbidden by the compiler:
unique_ptr<Result> result.reset(new AResult);
result->add_columns(...)
Upvotes: 1
Views: 142
Reputation: 96241
It looks to me like when you create it you know the type, so stash that off before assigning it into the unique_ptr
:
std::unique_ptr<AResult> temp(new AResult);
temp->add_columns(...);
unique_ptr<Result> result(std::move(temp));
Upvotes: 4