• Create Account

Posted 12 May 2013 - 04:25 AM

I am trying to design a component-based entity system and I came up with a solution that looks similar to this:

class base_component
{

};

template <class T>
class base_system
{
public:
protected:
std::map<int, T> m_components;
};

class spacial_component : public base_component
{
public:
};

class spacial_system : public base_system<spacial_component>
{

};


This way I was hoping the base_system class would create an std::map with the components for every component system.

What I am wondering is if it is valid to design it like this and then use polymorphism like the following:

spacial_system ss;

base_system<base_component>* bs = (base_system<base_component>*) ss;

My hope would be that this is possible since my entity_system class should store a vector of all available systems for dispatching messages to the systems using virtual functions. I have it implemented in VS 2012 and it works there. Then I tried to use the same code on VS 2010 and got random crashes. I suspect the cast to base_system<base_component>* is not actually valid C++. So my question is, is the cast valid? Should I use another solution?

Posted 12 May 2013 - 04:24 AM

I am trying to design a component-based entity system and I came up with a solution that looks similar to this:

class base_component
{

};

template <class T>
class base_system
{
public:
protected:
std::map<int, T> m_components;
};

class spacial_component : public base_component
{
public:
};

class spacial_system : public base_system<spacial_component>
{

};


This way I was hoping the base_system class would create an std::map with the components for every component system.

What I am wondering is if it is valid to design it like this and then use polymorphism like the following:

spacial_system ss;

base_system<base_component>* bs = (base_system<base_component>*) ss;

My hope would be that this is possible since my entity_system class should store a vector of all available systems for dispatching messages to the system. I have it implemented in VS 2012 and it works there. Then I tried to use the same code on VS 2010 and got random crashes. I suspect the cast to base_system<base_component>* is not actually valid C++. So my question is, is the cast valid? Should I use another solution?

PARTNERS