Hi,
This is a followup question on this topic. I didn't want to ressurect a old thread, so I made new one. I was wondering now that I have a interface template class that my RenderComponent class inherit from
template<typename>
class IBaseComponent :public BaseComponentTest
{
public:
static unsigned int Type(void)
{
static const int type = GenerateID();
return type;
}
protected:
};
and and this is how my testcomponent is
class RenderComponent : public IBaseComponent<RenderComponent>
{
public:
sf::Sprite sprite;
int renderlayer = 1;
private:
const std::string componentName = "RenderComponent";
template < class Archive>//, GameObject& go>
friend void save(Archive& archive, const RenderComponent& go);
template < class Archive>//, GameObject& go>
friend void load(Archive& archive, RenderComponent& go);
};
I got map of
std::Map<int,BaseComponentTest*>
and I have a function about like this
template<class T_S>
T_S* GetComponentTest()
{
std::map<int, BaseComponentTest*>::iterator it;
int a = IBaseComponent<T_S>::Type();
it = componentMapT.find(a);
if (it != componentMapT.end())
return (T_S*)it->second;
return NULL;
}
P.S
is this better than my old one where I used typeid()?
I have a add function to that look about like this
void GameObject::AddComponent(BaseComponentTest* componentToAttach)
{
//use map inbuild function to check if there is a
if (!componentMapT.count(componentToAttach->GenerateID()) > 0)//<---- Not "Working" as intended, It works the way it suppose to but not what I want :<
{
componentToAttach->attachOn(this);
componentMapT.insert(componentMapT.end(),
std::make_pair
(
//IBaseComponent<componentToAttach>::Type(),// ->GenerateID(),
componentToAttach
)
);
}
}
If I were to do it this way, I will get the BaseComponentTest type id instead of the one I want, which would be whichever the derivied class its suppose to be. I want to check if the component is already inside the map.
Should I do it this way?
template<class TS>
bool addComponent(BaseComponentTest* componentToAdd)
{
if (!componentMapT.count(IBaseComponent<TS>::Type() > 0)
{
componentToAttach->attachOn(this);
componentMapT.insert(componentMapT.end(),
std::make_pair
(
IBaseComponent<TS>::Type(),// ->GenerateID(),
componentToAttach
)
);
}
}
Or should I have a int inside my BaseComponentTest that the same as the
static const int type = GenerateID();
public int IDTYPE = type;
but its public so I can acess it outside of the class?