Is there a way to call the parent constructor with different arguments depending on the value of an argument that the child constructor has?
I have the following parent class:
class Rectangle
{
public:
Rectangle(std::string name, glm::vec3 top_left_corner, float height, float width, glm::vec3 color, bool fill);
~Rectangle();
//...
}
And the child class:
class Wall :
public Rectangle
{
public:
Wall(std::string name, Position position, float scene_height, float scene_width, float thickness, glm::vec3 color);
~Wall();
//...
}
Where Position
is an enum
that should dictate what arguments the parent constructor should be called with:
enum Position { UP, DOWN, LEFT, RIGHT };
So basically, I would like to have something like this in the child constructor:
Wall::Wall(std::string name, Position position, float window_height, float window_width, float thickness, glm::vec3 color) {
switch(position) {
case UP:
Rectangle(name, glm::vec3(0, window_height, 0), thickness, window_height, color, true);
break;
case DOWN:
Rectangle(name, glm::vec3(0, thickness, 0), thickness, window_width, color, true);
break;
case LEFT:
Rectangle(name, glm::vec3(0, window_height, 0), window_height, thickness, color, true);
break;
case RIGHT:
Rectangle(name, glm::vec3(0, window_width - thickness, window_height), window_height, thickness, color, true);
break;
}
}
But as you know, I have to call the parent constructor first, like:
Wall::Wall(std::string name, Position position, float window_height, float window_width, float thickness, glm::vec3 color)
: Rectangle(name, glm::vec3(0, window_width - thickness, window_height), window_height, thickness, color, true) {}
And that doesn't give me much leeway. What would a good, object-oriented approach be?