[英]C++ Array of pointers to class names
I'd like to be able to hold an array of class names and simply pass an index to a function which would create an instance of a certain class. 我希望能够持有一个类名数组,并且只需将索引传递给将创建某个类实例的函数即可。 I have memory constraints so I'd like to not simply create an array of objects.
我有内存限制,所以我不想简单地创建对象数组。
Here's my most descriptive pseudo-code: 这是我最具描述性的伪代码:
Classes: 类:
class Shape
{
public:
Shape();
};
class Square : public Shape
{
public:
Square();
};
class Triangle : public Shape
{
public:
Triangle();
};
Main: 主要:
int main()
{
pointerToShapeClass arrayOfShapes[2]; // incorrect syntax - but
arrayOfShapes[0] = pointerToSquareClass; // how would i do this?
arrayOfShapes[1] = pointerToTriangleClass;
cin << myNumber;
// Depending on input, create instance of class corresponding to array index
if (myNumber == 0) { // create Square instance }
if (myNumber == 1) { // create Triangle instance }
return 0;
}
If this is confusing, I can try to elucidate. 如果这令人困惑,我可以尝试阐明。 Thanks in advance!
提前致谢!
Edit: 编辑:
Really, I think I just need a pointer to a class without actually instantiating the class. 确实,我认为我只需要一个指向类的指针,而无需实际实例化该类。
Sounds like you want some kind of factory: 听起来您想要某种工厂:
class Shape
{
public:
Shape();
static Shape* create(int id);
};
Shape* Shape::create(int id) {
switch (id) {
case 0: return new Square();
case 1: return new Triangle();
}
return NULL;
}
Then when you want to create a particular Shape
given user input, you can do: 然后,当您要根据用户输入创建特定的
Shape
,可以执行以下操作:
int myNumber;
cin >> myNumber;
Shape* shape = Shape::create(myNumber);
This is it in it's simplest form. 这是最简单的形式。 I would, however, recommend having the
create
function return a std::unique_ptr<Shape>
, rather than a raw pointer. 但是,我建议让
create
函数返回std::unique_ptr<Shape>
,而不是原始指针。 I would also set up static constants to represent the different IDs. 我还将设置静态常量来表示不同的ID。
class Shape
{
public:
Shape();
static std::unique_ptr<Shape> create(int id);
enum class Id { Square = 0, Triangle = 1 };
};
std::unique_ptr<Shape> Shape::create(Shape::Id id) {
switch (id) {
case Shape::Id::Square: return new Square();
case Shape::Id::Triangle: return new Triangle();
}
return nullptr;
}
Create a std::vector<Shape*>
and then you can do v.emplace_back(new Triangle());
创建一个
std::vector<Shape*>
,然后可以执行v.emplace_back(new Triangle());
in C++11. 在C ++ 11中。 In C++03 you can use
v.push_back(new Triangle());
在C ++ 03中,您可以使用
v.push_back(new Triangle());
You could use a raw array 您可以使用原始数组
Shape* shapes[10]; // array of pointers;
shapes[0] = new Triangle();
You could also go to use templates and create a template 您也可以使用模板并创建模板
template<typename ShapeType>
class Shape
{
public:
ShapeType draw();
//All common Shape Operations
};
class Triangle
{
};
//C++11
using Triangle = Shape<Triangle>;
Triangle mytriangle;
//C++03
typedef Shape<Square> Square;
Square mysquare;
Try this: 尝试这个:
int main()
{
std::vector<Shape*> shapes;
cin << myNumber;
// Depending on input, create instance of class corresponding to array index
if (myNumber == 0) { shapes.push_back(new Square(); }
if (myNumber == 1) { shapes.push_back(new Triangle(); }
return 0;
}
I would create this function: 我将创建此函数:
Shape getNewShape(int shapeId)
{
switch (shapeId)
{
case 1: return new Square();
case 2: return new Triangle();
}
//here you should handle wrong shape id
}
then use it like that: 然后像这样使用它:
int main()
{
cin << myNumber;
// Depending on input, create instance of class corresponding to array index
shape tempShape = getNewShape(myNumber);
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.