Solved

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

Posted on 2014-04-05
13
726 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

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

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!

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

623 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