Jump to content
  • Advertisement
Sign in to follow this  
RenderCache

basic templates stuff

This topic is 5410 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hey! i was wondering if we could "simply" our usage of not having to type out one whole long templateID, in short, can we do somekind of typedef the here??
template<class T, class U, class V> class Base : public T, public U, public V
{
	
};
typedef Base<T, U, V> BaseOp;

template<class T, class U, class V> class Derived : public BaseOp
{
};

thx! RC

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by RenderCache
hey!
i was wondering if we could "simply" our usage of not having to type out one whole long templateID, in short, can we do somekind of typedef the here??


The closest you could probably get is by making a templated struct and putting your classes in there:


template<class T, class U, class V>
struct hierarchy
{
class Base {};
class Derived : Base {};
};





But then everytime you use the class from outside that struct you'd have to go:

hierarchy< some_class, some_other_class, you_get_the_point >::Derived


I say either just get used to typing it in, or make a macro.

Share this post


Link to post
Share on other sites
i see!
good stuff!

one other basic template qn:

can a vector hold "abtract template elements"?

eg, i read in a script, this script dictates what kind of policies i need at different times, and objects created from the same template, have different policies, and yet they must all be put into one same vector.

can this be done?

thx!
RC

Share this post


Link to post
Share on other sites
You'd do this using a base class with virtual functions.

class Base
{
virtual void DoSomething() =0;
};

template<typename T>
class Impl
{
virtual void DoSomething()
{
//...
}
};

std::vector<Base*> vectorOfBases;


That catch being that with templates you somehow need to know at *compile* time what instantiation of Impl you'll need that corresponds to the script-specified policy. You'll probably want to use the Factory pattern to help with this, but as a very rough example you could do:

Base* MakeImpl(const std::string& policyName)
{
if(policyName=="int") return new Impl<int>;
else if(policyName=="float") return new Impl<float>;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!