0

I have a class template Shape, which contains information about certain shapes (which can be three- or two-dimensional). I only want a few predefined shapes (cube, sphere and square) to be available. All these predefined shapes have the same properties (so the cube always has the same volume, and I only need to remember the properties of one cube). To inhibit someone from creating other Shapes, I made the constructor private:

// Flag for the possible shapes
enum class Tag
{
    SPHERE,
    CUBE,
    SQUARE
};

template<std::size_t N>
class Shape
{
public:
    // Predefined shapes.
    static const Shape<3> SPHERE;
    static const Shape<3> CUBE;
    static const Shape<2> SQUARE;
    // Information stored about the given shapes
    const Tag tag; // tag specifying the shape
    const double v; // Shape volume/area
    const std::array<double, 2*N> surrounding_box; // Storing intervals for a surrounding box
    //... Some other information that depends on template parameter N
private:
    // Private constructor. This prevents other, unintended shapes from being created
    Shape(Tag tag, double v, const std::array<double, 2*N> surrounding_box):
            tag{tag}, v {v}, surrounding_box {surrounding_box} {};
};

// Initialization of predefined shape: SPHERE
template<std::size_t N>
const Shape<3> Shape<N>::SPHERE(Tag::SPHERE, 3.0,{{0.0,2.7,0.0,2.7,0.0,2.7}});

// Initialization of predefined shape: CUBE
template<std::size_t N>
const Shape<3> Shape<N>::CUBE(Tag::CUBE, 1.0,{{0.0,1.0,0.0,1.0,0.0,1.0}});

// Initialization of predefined shape: SQUARE
template<std::size_t N>
const Shape<2> Shape<N>::SQUARE(Tag::SQUARE, 1.0,{{0.0,1.0,0.0,1.0}});

Now I can get a cube as:

Shape<3> cube = Shape<3>::CUBE;

This seems to works fine.

Problems arise when I want to have a Shape instance as member of another class template Object. Specifically, I do not manage to write a properly working constructor for my Object class template:

template <std::size_t N>
class Object
{
public:
    Object(Tag shape_tag, double weight, double elevation):
            weight {weight}, elevation {elevation}
    {
        switch(shape_tag)
        {
            case Tag::CUBE:
            {
                shape = Shape<3>::CUBE;
                break;
            }
            case Tag::SPHERE:
            {
                shape = Shape<3>::SPHERE;
                break;
            }
            case Tag::SQUARE:
            {
                shape = Shape<2>::SQUARE;
                break;
            }
        }
    }
private:
    Shape<N> shape;
    double weight;
    double elevation;
};

Creating an Object as

Object<3> object(Tag::CUBE, 1.0,1.0);

fails with the compiler error error: no matching function for call to ‘Shape<3ul>::Shape()’. I think that, because I do not use an initializer list for shape, the constructor of Object tries to call the default constructor Shape(), which is not available. I also tried moving the Shape part of construction to a separate initialization function, which I can then call in the initializer list. However, in that case the template part keeps generating different problems (because I need to be able to initialize both Shape<2> and Shape<3> objects).

How can I tackle this problem? Or is there perhaps a better way to make sure that only some predefined Shapes are available, without making its constructor private?

ps. The problem with shapes and objects as presented here is just a MWE.

JorenV
  • 373
  • 2
  • 10
  • 2
    having `static const Shape<3> SPHERE;` as a member in every instantiation of `template Shape` is a bit strange. I dont think this really is what you want – 463035818_is_not_an_ai Oct 12 '18 at 14:34
  • Note that `Object<2>(Tag::CUBE, 1.0,1.0);` would be problematic with mismatching dimension. – Jarod42 Oct 12 '18 at 14:39
  • @user463035818 I would like to have one `const Shape<3> SPHERE`, which I can store in a `Shape<3> shape` variable. But I thought that, because SPHERE is declared static, there would only be one SPHERE created and shared over all `Shape`s? – JorenV Oct 12 '18 at 14:40
  • note that `Shape<2>`, `Shape<3>`,`Shape<4>`, etc are all distinct types, and each of those types has a static member `Shape<3> CUBE` – 463035818_is_not_an_ai Oct 12 '18 at 14:46
  • @Jarod42 If in that case an exception was thrown, it would in principle be sufficient for me. Would it perhaps be better to create an `Object` as `Object(Shape shape, double weight, double elevation);` (so passing a class instance `shape` instead of a `Tag`? – JorenV Oct 12 '18 at 14:47
  • @user463035818 Ideally I would want `Shape<3>` to be either `CUBE` or `SPHERE` and `Shape<2>` to be `SQUARE` and in the future maybe something else I define. But using these static members was, after a long search, the only way I could find to limit the `Shape`s to some predefined instances. If you would know another way, I would be happy to hear about it :) – JorenV Oct 12 '18 at 15:00
  • actually i didnt read the question in all its detail, but I think the answer has already what you want – 463035818_is_not_an_ai Oct 12 '18 at 15:17

1 Answers1

3

Create a factory:

template <std::size_t N> Shape<N> MakeShape(Tag shape_tag);

template <>
Shape<3> MakeShape(Tag shape_tag)
{
    switch(shape_tag)
    {
        case Tag::CUBE: return Shape<3>::CUBE;
        case Tag::SPHERE: return Shape<3>::SPHERE;
    }
    throw std::runtime_error("Invalid tag");
}

template <>
Shape<2> MakeShape(Tag shape_tag)
{
    switch(shape_tag)
    {
        case Tag::SQUARE: return Shape<3>::SQUARE;
    }
    throw std::runtime_error("Invalid tag");
}

And then

template <std::size_t N>
class Object
{
public:
    Object(Tag shape_tag, double weight, double elevation):
shape{MakeShape<N>(shape_tag)}, weight {weight}, elevation {elevation}
    {
    }
};
Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • Thanks! This seems to work well! Do you perhaps also know a solution to the problem discussed in the comments to my questions (about the fact that in my implementation, every instance of `Shape` contains the `CUBE`, `SPHERE`,...? – JorenV Oct 12 '18 at 15:06
  • You have to specialize `Shape<2>` and `Shape<3>`, and possibly add a base `ShapeImpl` to factorize code. – Jarod42 Oct 12 '18 at 15:47