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?

5
  • 4
    You can't make an array of the objects themselves, but you can make an array of object pointers. Commented May 1, 2014 at 16:45
  • std::vecotr<Creature*> sounds like a good option to me. Commented May 1, 2014 at 16:46
  • You can probably make an array of Creature objects and cast them individually to their subclass, then get type for each object. Commented May 1, 2014 at 16:46
  • Anything against Creature ** theArray = {human, cyberDemon, balrog, elf};? (Sorry, forgot the variable name...) Commented May 1, 2014 at 16:47
  • 1
    @JoãoMendes Yes, we're talking about C++ here. Commented May 1, 2014 at 16:48

2 Answers 2

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)
};
Sign up to request clarification or add additional context in comments.

7 Comments

ahh the vector, was thinking I would need to use that. Thank you I appreciate it!
@KerrekSB Well, tidy except for that... I took the pointers as granted and looked only at the "make an array of different objects" problem :-)
@KerrekSB Added another version.
@iavr: Neat, though it isn't exception safe and probably doesn't compile, on account of explicit constructors.
@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: stackoverflow.com/questions/7038357/… (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.
|
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) };

4 Comments

I would post a C++11 version too, like std::vector<Creature *> creatureList = { new Human(100, 100), new Cyberdemon(100, 100), new Balrog(100, 100), new Elf(100, 100) };
@Chnossos: I think you need to remove the = before the brace.
@Apriori Both forms are valid. What you suggest is direct initialization while what I have in the answer is copy initialization.
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.