1

I am trying to make an array of different objects that are all inherited from one abstract class. Is this possible? Here's what I have:

Human *human;
human = new Human(100,100);

Cyberdemon *cyberDemon;
cyberDemon = new Cyberdemon(100, 100);

Balrog *balrog; 
balrog = new Balrog(100, 100);

Elf *elf;
elf = new Elf(100, 100);

Human and Elf get inherited from Creature which is an abstract class. Cyberdemon and Balrog get inherited from Demon class which inherits also from Creature. What is the best way to make an array of these four objects?

justin henricks
  • 437
  • 2
  • 6
  • 17

2 Answers2

4

Because I like code to be tidy:

Human      *human      = new Human(100, 100);
Cyberdemon *cyberDemon = new Cyberdemon(100, 100);
Balrog     *balrog     = new Balrog(100, 100);
Elf        *elf        = new Elf(100, 100);

std::vector<Creature*> creatureList{human, cyberDemon, balrog, elf};

Or, if you won't be needing individual pointers later:

std::vector<Creature*> creatureList{
    new Human(100, 100),
    new Cyberdemon(100, 100),
    new Balrog(100, 100),
    new Elf(100, 100)
};

Or, tidy and safe (thanks Kerrek SB):

std::vector<unique_ptr<Creature>> creatureList{
    std::make_unique<Creature>(100, 100),
    std::make_unique<Cyberdemon>(100, 100),
    std::make_unique<Balrog>(100, 100),
    std::make_unique<Elf>(100, 100)
};
iavr
  • 7,277
  • 1
  • 13
  • 50
  • ahh the vector, was thinking I would need to use that. Thank you I appreciate it! – justin henricks May 01 '14 at 16:55
  • @KerrekSB Well, tidy except for that... I took the pointers as granted and looked only at the "make an array of different objects" problem :-) – iavr May 01 '14 at 17:01
  • @KerrekSB Added another version. – iavr May 01 '14 at 17:04
  • @iavr: Neat, though it isn't exception safe and probably doesn't compile, on account of explicit constructors. – Kerrek SB May 01 '14 at 17:12
  • @Kerrek SB:I realize this is probably where you were going with this but one could make this exception safe if one provides an implementation for make_unique as described here: http://stackoverflow.com/questions/7038357/make-unique-and-perfect-forwarding (making it a hyperlink wasn't working, look at the link in the chosen answer). However, this is probably outside the scope of OPs question, and somewhat of an advanced topic, especially if OP is just learning C++ polymorphism. – Apriori May 01 '14 at 17:19
  • @KerrekSB/Apriori Still, my apologies at least for non-compiling; started as an easy, quick answer. I hope it's ok now. – iavr May 01 '14 at 17:31
  • Cool, thanks. Just about the "advanced" nature -- if the OP is "just learning C++", I think it's actually a hundred times more important to not taint his young, mouldable mind with bad practices, and instead show how it's done properly. We have too many people with terrible "my professor told me so" code... – Kerrek SB May 01 '14 at 18:24
3
std::vector<Creature*> creatureList;

Human *human;
human = new Human(100,100);
createList.push_back(human);

Cyberdemon *cyberDemon;
cyberDemon = new Cyberdemon(100, 100);
createList.push_back(cyberDemon);

Balrog *balrog; 
balrog = new Balrog(100, 100);
createList.push_back(barlog);

Elf *elf;
elf = new Elf(100, 100);
createList.push_back(elf);

Or, a little bit simplified:

std::vector<Creature*> creatureList;

createList.push_back(new Human(100,100));
createList.push_back(new Cyberdemon(100, 100));
createList.push_back(new Balrog(100, 100));
createList.push_back(new Elf(100, 100));

If you are able to use C++11, you can simplify it a little further (thanks Chnossos).

std::vector<Creature*> creatureList = { new Human(100,100), 
                                        new Cyberdemon(100, 100),
                                        new Balrog(100, 100),
                                        new Elf(100, 100) };
R Sahu
  • 196,807
  • 13
  • 136
  • 247
  • I would post a C++11 version too, like `std::vector creatureList = { new Human(100, 100), new Cyberdemon(100, 100), new Balrog(100, 100), new Elf(100, 100) };` – Chnossos May 01 '14 at 16:51
  • @Chnossos: I think you need to remove the `=` before the brace. – Apriori May 01 '14 at 18:19
  • 1
    @Apriori Both forms are valid. What you suggest is direct initialization while what I have in the answer is copy initialization. – R Sahu May 01 '14 at 18:24
  • @R Sahu: Thank you for the clarification. I am reading about the syntax now, so far the article has only said there is no `=`. I appreciate the supplementary knowledge. – Apriori May 01 '14 at 18:33