I am new to C++ and trying to make list polymorphic/accepting anything deriving from a base class. The issue is that this list must be private, using separate methods to append and interrogate it.
After some research, I was able to get close in a safe manner through smart pointers.
Here is what I have arrived at:
class Shape
{
public:
Shape(std::string name)
{
this->name = name;
}
std::string name;
std::string getName(void)
{
return this->name;
}
};
class ShapeCollector
{
public:
void addShape(Shape shape)
{
this->shapes.push_back(std::make_unique<Shape>("hey"));
}
private:
std::vector <std::unique_ptr<Shape>> shapes;
};
I would like to be able to replace the make_unique call with the shape parameter, however nothing I try seems to play correctly.
I could create each derived class inside ShapeCollector, mirroring the constructor arguments as parameters, but this feels very counter intuitive.
Any help would be appreciated!