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
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
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.
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 ::operatornew(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.