没有公共构造函数作为另一个类模板的成员的类模板 [英] class template without public constructor as member of another class template

查看:85
本文介绍了没有公共构造函数作为另一个类模板的成员的类模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类模板 Shape ,其中包含有关某些形状(可以是3维或2维)的信息。我只希望提供一些预定义的形状(立方体,球体和正方形)。所有这些预定义的形状都具有相同的属性(因此,多维数据集始终具有相同的体积,我只需要记住一个多维数据集的属性)。为了禁止某人创建其他 Shape s,我将构造函数设为 private

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;

这似乎很好。

当我想让 Shape 实例作为另一个类模板 Object 的成员时出现问题。具体来说,我无法为 Object 类模板编写可正常使用的构造函数:

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;
};

创建对象

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

因编译器错误 error失败:没有匹配的函数调用'Shape< ; 3ul> :: Shape()'
我认为,因为我没有为 shape 使用初始化列表,所以 Object 的构造函数会尝试调用默认构造函数 Shape(),该构造函数不可用。
我还尝试将构造的 Shape 部分移动到单独的初始化函数,然后可以在初始化器列表中调用该函数。但是,在那种情况下,模板部分不断产生不同的问题(因为我需要能够初始化 Shape< 2> Shape< 3> 对象)。

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).

如何解决此问题?还是有更好的方法来确保仅提供一些预定义的 Shape 而不将其构造函数设为私有?

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。

推荐答案

创建工厂:

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");
}

然后

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}
    {
    }
};

这篇关于没有公共构造函数作为另一个类模板的成员的类模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆