Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-04-05
13
Medium Priority
?
747 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 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 35

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

971 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