std::vector and derived classes

Sup guys,

I've created a base class called "SDLGameObject". The "Player"-class and the "Enemy"-class are derived classes from SDLGameObject. There will be lots of enemy typs and I'd like to iterate through the vector and render all the enemies in one process (ofc there'll be a "isonScreen"-bool to prevent wasting time and energy).

What I'd like to do is a vector of type "Enemy".
std::vector<Enemy> EnemyList;

How can i push_back the objects of the derived class? Is what I want even possible (guess it is)?
1
2
3
4
5
Dwarf Dwarf1; //Object of "Dwarf"-class, derived from "Enemy"-class
Elf Elf1; //Object of "Elf"-class, derived from "Enemy"-class 
EnemyList.push_back(&Dwarf1);
EnemyList.push_back(&Elf1);
//interate trough vector in the Load(), Update() and Render() function... 


I'm positive that this Topic was already being brought up, but I guess I'm not finding the right words for Google. Maybe its somehow related to this Topic http://www.cplusplus.com/forum/general/55651/ , but srsly I don't understand the answers xD

Thanks for help
You want a vector of pointers:
1
2
3
4
std::vector<std::unique_ptr<Enemy>> enemies;

enemies.emplace_back(new Dwarf);
enemies.emplace_back(new Elf);
Alright :) Thanks for the fast answer!

Pointers and inheritance aren't exactly where I'm good at ^^'

So your are using the new keyword... If I'd have a constructor with arguments it would be the same except (new Dwarf1(50,200));?

is the Expression in parenthesis just telling the Compiler that there is going to be a dwarf-object there or is the Dwarf already an object? So I could say
1
2
enemies.emplace_back(new Dwarf1);
enemies.emplace_back(new Dwarf2);
?
Last edited on
Yes, you can construct it with arguments using both styles of initialization:
6
7
enemies.emplace_back(new Dwarf(1, 2));
enemies.emplace_back(new Dwarf{3, 4});


The expression in parenthesis is the first an only parameter being bassed to the .emplace_back() member function of std::vector. As for the example code you provided, I am not sure what you're trying to point out? If you're asking "can I use different names for things", the answer is yes.
Alright :)

What I mean by my second question:

normally you'd do something like this
1
2
3
Dwarf* Dwarf1;

Dwarf1 = new Dwarf(50,300);


but you don't there... So you can answer my last question just by saying if this is correct ^^
1
2
3
4
enemies.emplace_back(new Dwarf1);
enemies.emplace_back(new Dwarf2);
enemies.emplace_back(new Elf4);
enemies.emplace_back(new Elf216564);


Do I have to declare those objects somewhere before i can use emplace_back() or is this the declaration itself?

Objects are not declared. Variables are declared. You don't need variables to have objects.
Hmm i don't get along with the english Terms either >.<

Anyway I'm home and with your help and a bit of tryouts everythings fine and clear to me ;)

Thanks a lot LB!
It's not so much an English thing, it's a C++ thing ;)
It might help to think of new as a function that takes a type as an argument (in reality it's an operator which takes size_t as an argument) and returns a pointer to a constructed object.

So when you do...
new Dwarf1
It ends up being
::operator new(sizeof(Dwarf1))

If that makes sense to you, then it should also make sense that you don't need to declare anything ahead of time.
But my programmer english is messed up ^^'

Thanks to the two of you, I think I've understood everything :DD
Last edited on
Topic archived. No new replies allowed.