Coming from the Java world, I'm trying to accomplish something similar to
import java.util.List;
class BaseClass {
protected List<? extends BaseClass> members;
}
class ClassA extends BaseClass {
public ClassA(List<ClassA> args) { // I want to have only ClassA objects here
this.members = args;
}
}
Now in C++, the following doesn't compile because of the mismatch
#include <vector>
#include <memory>
class BaseClass {
protected:
std::vector<std::shared_ptr<BaseClass>> children;
};
class ClassA : public BaseClass {
explicit ClassA(const std::vector<std::shared_ptr<ClassA>>& args) {
this->children = args; // error here
}
};
The compilation error is
error: no match for ‘operator=’ (operand types are ‘std::vector<BaseClass>’ and ‘const std::vector<ClassA>’)
11 | this->children = args;
| ^~~~
What would be the most idiomatic/cleanest solution? I'm using C++20.