2013-03-12 127 views
1

我想能够容纳一个类名的数组,并且只需将一个索引传递给一个可以创建某个类的实例的函数。我有内存限制,所以我不想简单地创建一个对象数组。C++指向类名称的指针

这里是我最描述伪代码:

类:

class Shape 
{ 
public: 
    Shape(); 
}; 

class Square : public Shape 
{ 
public: 
    Square(); 
}; 

class Triangle : public Shape 
{ 
public: 
    Triangle(); 
}; 

主营:

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

如果这是混乱的,我可以尝试阐明。提前致谢!

编辑:

真的,我想我只需要一个指向类,而无需实际实例化类。

+0

你不能在你想要的方式来做到这一点。你也许可以对模板有点聪明,并找出一些东西。 – antonijn 2013-03-12 15:53:05

+0

虽然我知道你在发布伪代码,但请记住,你会想让你的基类是虚拟的,所以你可以实际调用基类指针的方法,并让它们正确地调用派生类的方法。 – Jason 2013-03-12 15:56:09

回答

3

听起来像你想要某种工厂:

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

然后,当你想创建一个特定的Shape给定用户的输入,你可以这样做:

int myNumber; 
cin >> myNumber; 
Shape* shape = Shape::create(myNumber); 

这是它在它的简单形式。但是,我会建议让create函数返回一个std::unique_ptr<Shape>,而不是原始指针。我也会设置静态常量来表示不同的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; 
} 
+0

正是我在找的!谢谢! – 2013-03-12 16:10:11

2

创建一个std::vector<Shape*>然后你可以在C++ 11中做v.emplace_back(new Triangle());。在C++ 03可以使用v.push_back(new Triangle());

你可以使用原始阵列

Shape* shapes[10]; // array of pointers; 

shapes[0] = new Triangle(); 

你也可以去使用模板和创建模板

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; 
+0

托尼,我正在Arduino上实现这一点,因此将无法使用矢量。 – 2013-03-12 15:55:08

+0

@RyanTuck我在模板或指针的原始数组中放入一个建议 – 2013-03-12 15:57:27

+0

'Shape * [10] shapes' < - 这不是有效的C++语法。另外,可能建议使用'unique_ptr'而不是原始指针。手动管理内存实际上并不重要。 – 2013-03-12 16:06:43

2

试试这个:

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

(+1)虽然我会使用'switch' ... – 2013-03-12 16:01:31

+0

Roee,你是绝对正确的。:) – Silas 2013-03-12 16:07:00

+0

这个泄漏。对于我的“试试这个”答案的立场,你可能想看看我的个人资料页面。 – 2013-03-12 16:13:02

1

我会创造这样的功能:

Shape getNewShape(int shapeId) 
{ 
    switch (shapeId) 
    { 
     case 1: return new Square(); 
     case 2: return new Triangle(); 
    } 
    //here you should handle wrong shape id 
} 

然后用它这样的:

int main() 
{ 
    cin << myNumber; 

    // Depending on input, create instance of class corresponding to array index 
    shape tempShape = getNewShape(myNumber); 

    return 0; 
}