Array of base objects filled with derived objects..

I was just trying to figure out why this code doesn't work and what would be the best way to go about doing this?

 
NPC** oEnemies = new Pig*[numPigs];


NPC is the base class and Pig is derived from it. I want to be able to just do something like this in the end.

1
2
3
//Within the logic part of my game loop
for(int i = 0; i < numEnemies; i++)
    oEnemies[i]->AI();

AI() is a virual function from NPC and the Pig class has it's own AI().

Edit:
I meant to mention that just declaring..
NPC* oEnemies = new Pig;
works fine.
Last edited on
Why are you using a double pointer? Couldn't you just write
NPC* oEnemies = new Pig[numPigs];?
Just do
 
NPC** oEnemies = new NPC*[numPigs];
I do not see a greate sense in statement

NPC** oEnemies = new Pig*[numPigs];

You should change the code below the following way

1
2
3
//Within the logic part of my game loop
for(int i = 0; i < numEnemies; i++)
    oEnemies[i]->AI();
that's not the actual code I have and you're right about oEnemies should have the [i] I just didn't notice for this lol... anyway.

Peter NPC is a base class and cannot have an instance of itself.

I will try that Zhuge but the idea is to have pointer to each element of the array not the array itself.

Why can't I have

1
2
 NPC** oEnemies = new Pig*[numPigs];
 


...this works fine but doesn't allow me to store all my devired NPC types.
 
Pig** oPig = new Pig*[numPigs];


Edit: zhuge, I tested what you said out and it is what I thought. It doesn't create pointers to each element of the array only the array itself which isn't what I need.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <cstdlib>
#include <ctime>
#include <memory>
#include <vector>
#include <iostream>

struct NPC 
{
    virtual void AI() =0 ;
};

struct Pig : public NPC
{
    void AI() { std::cout << "Not by the hair of my chinny chin chin!\n" ; }
};

struct Wolf : public NPC
{
    void AI() { std::cout << "Little pig, little pig!  Let me in!\n" ; }
};


const unsigned numInitialEnemies = 10 ;

int main()
{
    std::srand(std::time(0)) ;

    std::vector<std::unique_ptr<NPC>> enemies ;

    for ( unsigned i=0; i<numInitialEnemies; ++i )
    {
        if ( rand() % 2 )
            enemies.emplace_back(new Pig) ;
        else
            enemies.emplace_back(new Wolf) ;
    }

    for ( unsigned i=0; i<enemies.size(); ++i )
        enemies[i]->AI() ;
}


http://ideone.com/ZJa3jN
Exempt wrote:
Peter NPC is a base class and cannot have an instance of itself.


new NPC*[numPigs] doesn't create any NPC instances. It creates NPC pointers, which is fine because NPC pointers can point to Pig instances.
Ah, i see what you mean peter, thanks for clearing it up. Initialize the npc array with new npc's then store my derived pigs or whatever inside. I guess that answers that and i'll need to use a vector list or map to store them the right way i guess like cire has shown.

Thanks for all the replies everyone.
Topic archived. No new replies allowed.