简体   繁体   English

C ++-指向类的指针数组

[英]C++ - Array of pointers to classes

I hope someone here can help me. 我希望这里有人可以帮助我。

So this is my problem: I have a main class and three other classes. 这就是我的问题:我有一个主班和另外三个班。 Now I want to make an array with each index pointing to one of these classes. 现在,我想创建一个数组,每个索引指向这些类之一。 I know how to do it with one class like this: 我知道如何通过这样的一门课来做到这一点:

Class1* array[10];

and then I can use this for every index: 然后我可以将其用于每个索引:

array[i] = new Class1;

But is it possible to declare an array and then use something like this? 但是可以声明一个数组然后使用类似的东西吗?

array[0] = new Class1;
array[1] = new Class2;
array[2] = new Class3;

Kind regards, Synotix 亲切的问候,Synotix

It could be possible if you use a main class as parent and the classes Class1, Class2 and Class3 should be subclasses of the main class. 如果您将主类用作父类Class1, Class2 and Class3类应该是主类的子类,则有可能。

If all your classes (1-3) extend from the class eg ParentClass you could write: 如果您所有的类(1-3)都从该类(例如ParentClassextend而来,您可以编写:

ParentClass* array[n];

ParentClass* class1 = new Class1();
ParentClass* class2 = new Class2();
ParentClass* class3 = new Class3();
//...
array[0] = class1;
//... classes 2 and 3

Here is an example how to extend classes in c++. 这是一个如何在c ++中扩展类的示例

Yes You can do it But for this Purpose You have to Use Inheritance and Polymorphism in C++. 是的,您可以这样做,但是为此,您必须在C ++中使用继承和多态。

What you can do is. 你能做的是。 Create a parent class ie 创建一个父类,即

Class parent {

// Some common code here
// Some virtual function here
}

Then inherit all of your class like in your case which are Class1 , Class2 , Class3 from parent class. 然后从您的类继承您的所有类(如您的情况),分别是Class1,Class2,Class3。 Also create a virtual method in the parent class to obtain polymorphism in C++. 还要在父类中创建一个虚拟方法以获得C ++中的多态性。

In this way You will be able to do this. 这样,您将可以执行此操作。

parent * allClasses[10];

allClasses[1]= new Class1();
allClasses[2]= new Class2();
allClasses[3]= new Class3();

Here is a complete Example of what You want. 这是您想要的完整示例。 Check this Link Can someone explain the benefits of polymorphism? 检查此链接有人可以解释多态的好处吗?

It is possible but what do you want to do with the array? 有可能,但是您想对阵列做什么? If your classes all have a common base class then you can define your array to contain pointers to that base class then you can add classes to the array as you have done. 如果您的所有类都有一个通用的基类,则可以定义数组以包含指向该基类的指针,然后可以将类添加到数组中。

In order to use your classes you will need to do it via virtual methods on the base class. 为了使用您的类,您将需要通过基类上的virtual方法来实现。

A few suggestions: I would suggest your base class has a virtual destructor, this means that when you delete the elements of the array it will call the destructor on the derived class. 一些建议:我建议您的基类具有virtual析构函数,这意味着当delete数组的元素时,它将在派生类上调用析构函数。 Also, I suggest using smart pointers instead of raw pointers so you don't need to call delete . 另外,我建议使用智能指针而不是原始指针,因此您无需调用delete Here is an example, I have used a vector instead of an array and a class template to define the classes: 这是一个示例,我使用了vector而不是数组和类模板来定义类:

#include <memory>
#include <vector>
#include <iostream>

class MyBase {
 public:
  virtual ~MyBase() {}
  virtual void hello() = 0;
};

template<int N>
class MyClass : public MyBase {
 public:
  virtual void hello(){ std::cout << "Hello, I am Number" << N << std::endl; }
};

int main() {
  std::vector<std::unique_ptr<MyBase>> my_classes;

  my_classes.emplace_back(new MyClass<1>());
  my_classes.emplace_back(new MyClass<2>());
  my_classes.emplace_back(new MyClass<3>());

  for(auto& my_class : my_classes)
    my_class->hello();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM