I am taking the step of moving on from Java over to c++, I have been working on my own custom game engine in java for a while so have a good understanding of polymophism, inheritance etc.

Now to the issue:

Currently I am working on a basic level format trying to get entities to render to the screen, this all goes well if i change the code to what i don't want it to be below is the code:

#include <SFML/Graphics.hpp>
#include "../Entities/Player.h"

#include "Layer.h"

class TileMap: public sf::Drawable, public sf::Transformable {
    std::vector<Player> entities;

    Player* addEntity(Player player) {
        return &entities[entities.size() - 1];

    void update() {
        for (unsigned int i = 0; i < entities.size(); i++) {

    virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const {
        for (unsigned int i = 0; i < entities.size(); i++) {


The player that is returned in the function is then stored in a DrawableEntiity* player; variable. & this works fine everything updates

As I want a generalized list of entities and not a list for each entity i add to the game i want to be able to change this from

std::vector<Player> entities;

and change it too (obviously including the include for it too)

std::vector<DrawableEntity> entities;

The issue is everytime i store it this way the draw and update functions of the lower level Entity class isn't called and instead the default function inside of the DrawableEntity is called.

This is probably a easy mistake on something I've overlooked but i can't find anything online anywhere that fixes the issue. a full code listing of the Entity classes can be found at http://pastebin.com/3rRuzZHs

Thanks for taking your time to read this Jordan

P.S: If you need any more information let me know.


When storing items of a child class in a vector, the vector will call the father's constructor (constructor of the type the vector was appointed to.)

To avoid that, you'll want a vector of pointers instead:

 std::vector<DrawableEntity *> entites;

For this approach you will likely have to dynamically allocate your entities manually using, say, the new keyword of course.

| improve this answer | |
  • 1
    \$\begingroup\$ While it is not wrong to use raw pointers I would recommend using smart pointers which make handling of memory easier. Especially if one comes from Java. Raw pointers should only be used when they can't be avoided at all costs. \$\endgroup\$ – Skalli Jun 30 '16 at 15:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.