Solved

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

Posted on 2014-04-05
13
696 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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 32

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

743 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