问题
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 Shape
s, 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 Shape
s are available, without making its constructor private?
ps. The problem with shapes and objects as presented here is just a MWE.
回答1:
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}
{
}
};
来源:https://stackoverflow.com/questions/52781740/class-template-without-public-constructor-as-member-of-another-class-template