2013-03-12 80 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; 
}