Jump to content
  • Advertisement
Sign in to follow this  
darknebula42

A vector filled with templates or whatnot.

This topic is 4284 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

I'm creating a configuration file loader and I want to create a vector of a data class that happens to be a template of the data that was loaded from the configuration file (Ex, int, string, doubles, user defined types, etc.) I also wanted the data class to hold a pointer to the variable that would keep up with the changes as the game went on, so when I went to write and load, it would be almost seamless. Is there anyway to keep a vector of a template class?

Share this post


Link to post
Share on other sites
Advertisement
Sure.

std::vector<type<T> > vectorName;


As for a vector that holds variant types? That's messy and ill-advised. Boost though has variant classes if you're deadset upon it.

Share this post


Link to post
Share on other sites

struct Holder
{
virtual int asInt() = 0;
virtual std::string asString() = 0;
};

template <typename T>
struct TypedHolder : Holder
{
virtual int asInt()
{
return m_value;
}
virtual std::string asString()
{
// bad way to convert, not even sure if it works
return std::string (itoa(m_value,10));
}

private:
T m_value;
}

template <>
struct TypedHolder<std::string>
{
virtual int asInt()
{
throw // bad cast
}
virtual std::string asString()
{
return m_value;
}
private:
std::string m_value;
}

std::list< Holder * > values;





But obviously, the better choice here would be boost classes, which already provide all of that. Any or Variant classes will give you all this in very robust form.

In addition, you have the serialization and program-options which can take care of all persistence and configuration.

[Edited by - Antheus on March 27, 2007 8:58:20 AM]

Share this post


Link to post
Share on other sites
Yeah, I thought I was going to have to do something like that. I've never heard of the Boost libs though. Seems pretty cool, thanks for the infos. Would you recommend using this library for the others things inside of it too? Do you use the lib?

Share this post


Link to post
Share on other sites
The Boost library is awesome, containing all sorts of useful stuff. Some of it is also likely to become part of the C++ Standard Library when the next standard is finally released (2009-ish, I believe?).

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!