简体   繁体   中英

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

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.

If all your classes (1-3) extend from the class eg ParentClass you could write:

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++.

Yes You can do it But for this Purpose You have to Use Inheritance and Polymorphism in 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. Also create a virtual method in the parent class to obtain polymorphism in 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.

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. Also, I suggest using smart pointers instead of raw pointers so you don't need to call delete . Here is an example, I have used a vector instead of an array and a class template to define the classes:

#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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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