Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


How to add a template class object into a map? and how stupid is my way?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 Conny14156   Members   -  Reputation: 274

Like
0Likes
Like

Posted 20 June 2014 - 12:07 PM

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?


Edited by Conny14156, 20 June 2014 - 12:08 PM.


Sponsor:

#2 Andy Gainey   Members   -  Reputation: 2310

Like
2Likes
Like

Posted 20 June 2014 - 12:28 PM

At the time of getting the type ID, if all you have is a pointer to BaseComponentTest, then you need your Type() function to be a virtual member function of both BaseComponentTest and IBaseComponent<T>.  The fact that it is a static function on IBaseComponent<T> (and I don't know how you defined it on BaseComponentTest) means that from a BaseComponentTest pointer, the program has no way of running the code for the specific component, and thus returning the correct ID.  Static functions are useful when you know the type in question at compile time.  In this case, that's clearly not the case.

 

It is easy to think that member functions are relevant only when you need to access data within an instance of the class, and it might seem at first glance that the type is independent of the instance.  It's ID is stored (reasonably) in a static variable, after all, not in a member variable that is a part of every single instance.  But what is less obvious is that the type of an instance is also stored as part of the instance, and is accessible through virtual member functions.  If you want to understand this at a deeper level, read up a bit on vtables.  Essentially, every class (that needs it, i.e., has virtual functions) stores a hidden member variable that points to additional information related to the type of that instance.  Without access to that hidden member variable through a virtual function, you have no way of accessing that information, and thus of finding out what type the instance is.


Edited by Andy Gainey, 20 June 2014 - 12:29 PM.


"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS