Petar Putar Visnjic Petar Putar Visnjic - 1 month ago 14
C++ Question

Storing classes in boost::variant

Someone recommended me to use boost::variant as shape variable to store different types of shapes in it. But, when implemented boost::variant to my code, I got an error while compiling. Error says: 'Shape': base class undefined and more errors.

Here is my code (Object.h):

using Shape = boost::variant<Rectangle, Circle>;

enum Shape_Type
{
RECTANGLE,
CIRCLE
};

struct Position
{
float x, y;

Position(float position_x, float position_y)
{
x = position_x;
y = position_y;
}
};

class Object : private Shape
{

private:
std::string name;

public:
Object() = default;

Object(std::string name, Rectangle rectangle) : name(name), Shape(rectangle)
{
}

Object(std::string name, Circle circle) : name(name), Shape(circle)
{
}

void setPosition(float, float);

void setAngle(float);

Shape* getShape()
{
Shape* shape = this;
return shape;
}

Position getPosition();

const std::string* getName()
{
return &name;
}
};

class Rectangle
{

private:
sf::RectangleShape rectangleshape;

public:
Rectangle() = default;

Rectangle(float width, float height)
: rectangleshape(sf::RectangleShape(sf::Vector2f(width, height)))
{
}

void setPosition(float position_x, float position_y)
{
rectangleshape.setPosition(position_x, position_y);
}

void setAngle(float angle)
{
rectangleshape.setRotation(angle);
}

sf::RectangleShape* getRectangleShape()
{
return &rectangleshape;
}

Position getPosition()
{
return Position(rectangleshape.getPosition().x,
rectangleshape.getPosition().y);
}
};

class Circle
{

private:
sf::CircleShape circleshape;

public:
Circle() = default;

Circle(std::string name, float radius)
: circleshape(sf::CircleShape(radius))
{
}

void setPosition(float position_x, float position_y)
{
circleshape.setPosition(position_x, position_y);
}

void setAngle(float angle)
{
circleshape.setRotation(angle);
}

sf::CircleShape* getCircleShape()
{
return &circleshape;
}

Position getPosition()
{
return Position(circleshape.getPosition().x,
circleshape.getPosition().y);
}
};


And btw is getShape() function good?

Answer

Variants are used for static polymorphism, so you don't need the base class at all (that's dynamic - or virtual - polymorphism).

The members in a variant typically do not share a common base class, so you wouldn't have the getShape function, or you'd need to template it:

template <typename T>
T const& getShape() const { return boost::get<T>(_shape); }