Link to home
Start Free TrialLog in
Avatar of pcomb
pcombFlag for United States of America

asked on

C++ hetergeneous list as an array of pointers to a abstract class getting c2259

I have an abstract class. I need to create a dynamic array of pointers to the class (or at least its derived classes). The abstract class is person

I am doing the following but getting c2259 cannon instantiate abstract class.

I know I cannot instantiate the class but can use with pointers. How can I create the array of pointers dynamically without using the base class Person?


Person* filelist = new Person[size];
Avatar of jkr
jkr
Flag of Germany image

You need to derive from person and use that implementation to create the array, e.g.

class Woman : public Person {

  // override all abstract methods with a proper implementation
};

Person* filelist = new Woman[size]; 

Open in new window

Avatar of pcomb

ASKER

my challenge is that the array could be made up of different derived classes eg man, woman, boy, girl.

If I want to dynamically create an array of 10 persons and then fill them with derived objects I cannot use woman as man or boy would ont work?

thx
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pcomb

ASKER

thank you I was missing the pointer ref on the right hand side. Unfortunately its throwing a different error for

Person* filelist = new Person*[size];
c2240 initializing cannot convert Person ** to Person *


filelist = new Woman(in, first_name, last_name);
c2259 Person cannot instantiate abstract class

Avatar of phoffric
phoffric

Just a typo:
Person** filelist = new Person*[size];
Avatar of pcomb

ASKER

perfect thanks
You should consider
vector<Person*> filelist (size);
Not a typo, but a copy&paste accident - sorry about that :-/
This is OT, but don't forget to use virtual destructors, and delete all your array elements before deleting filelist.
typo.. copy&paste accident - all the same. :)
This is OT, but don't forget to use virtual destructors, and delete all your array elements before deleting filelist.
a good way to do that is to create a class which handles the array of base class pointers and deletes all the pointers in the destructor.

the class additionally could implement the factory pattern what would allow to create new derived instances dynamically and add them to the array of persons.

typedef Person * (*CreatePerson)();

class Persons
{
     static std::map<std::string, CreatePerson> factory;
     
     std::vector<Person*> persons;

public:
     static bool addToFactory(const std::string& kind,CreatePerson createFunc);
     static Person* create(const std::string& kind);
  ~Persons() { for (int n = 0; n < (int)persons.size(); ++n) delete persons[n]; } 

    Person * addNewPerson(const std::string & kind);
    size_t size();
    const Person * operator[](int idx) const;
    Person * operator[](int idx);
};

Open in new window


for each derived class from class Person you would provide a static create function of your class which returns a Person pointer of a class object created by new operator. the create function (pointer) was added to factory map and can be used to create new instances of any derived class by name which added itself to factory.

Persons allPersons;
...
Person * pWoman = allPersons.addNewPerson("Woman");
// now use virtual calls 

Open in new window


Sara