Solved

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

Posted on 2014-04-05
13
721 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 34

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

751 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