Solved

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

Posted on 2014-04-05
13
702 Views
Last Modified: 2014-04-11
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];
0
Comment
Question by:pcomb
  • 4
  • 3
  • 3
  • +1
13 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 39980630
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

0
 

Author Comment

by:pcomb
ID: 39980635
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
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 39980639
Yup, Got that. But that sounds like you need a pointer array istead of a regular one, since otherwise the principles of abstract base classes won't work, i.e.

class Man : public Person {

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

class Woman : public Person {

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

Person** filelist = new Person*[size]; 

for (int i = 0;  < size; ++i)  { // fill up elements, 50% Man objects, 50% Woman objects

  if (i % 2)
    flelist[i] = new Man;
   else
    flelist[i] = new Woman;    
}
                                            

Open in new window


<<EDIT>> Added * to fix copyAndPaste error in line 11 - Paul, ZA
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:pcomb
ID: 39980654
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

0
 
LVL 32

Expert Comment

by:phoffric
ID: 39980655
Just a typo:
Person** filelist = new Person*[size];
0
 

Author Comment

by:pcomb
ID: 39980658
perfect thanks
0
 
LVL 32

Expert Comment

by:phoffric
ID: 39980662
You should consider
vector<Person*> filelist (size);
0
 
LVL 86

Expert Comment

by:jkr
ID: 39980663
Not a typo, but a copy&paste accident - sorry about that :-/
0
 
LVL 32

Expert Comment

by:phoffric
ID: 39980672
This is OT, but don't forget to use virtual destructors, and delete all your array elements before deleting filelist.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 39980794
typo.. copy&paste accident - all the same. :)
0
 
LVL 33

Expert Comment

by:sarabande
ID: 39985677
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
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now