   Different Vector Types in one Vector
  Let's say I have a vector:std::vector<typename> bob;Is there any possible way to make it where the vector can hold different types like int, long, etc.?   
  There isn't. Why do you want to do this, anyway? What are you trying to accomplish?   ;Quote:Original post by Matthew ShockleyLet's say I have a vector:std::vector<typename> bob;Is there any possible way to make it where the vector can hold different types like int, long, etc.?Not as such; SC++L containers are homogenous, meaning that all elements must be of the same type.However, there are ways to create the effect of heterogenous containers in C++. One way is polymorphism, but since you mention primitive types, that probably wouldn't be an appropriate solution here (at least in typical 'base and derived classes' form).There are a couple of other solutions, which are more or less exemplified by boost::any (which uses polymorphism under the hood) and boost::variant (which offers functionality similar to a C-style union). Either of these methods could be used to (in effect) store ints, longs, floats, and so on in a single SC++L container.What is this for exactly? Depending on what the problem is, there might be other solutions available.   ;  I am trying to make a simple thing like this:MyCoolClass bob;if (bob.HasMember("Name")) {bob.ChangeMemberValue("Name", 5);bob.ChangeMemberValue("Name", "Hi");bob.ChangeMemberValue("Name", true);}cout << bob.GetMember("Name");I want something like this.   ;  C++ isn't dynamic typed, therefore, you need to create this functionality yourself:#include <map>#include <string>#include <iostream>#include <iomanip>#include <sstream>#include <exception>#include <stdexcept>using namespace std;class DynamicClass {public:// Converts val to a string, and stores the // resulting string to member "key"template<typename T>void set( const string& key, const T& val ) {stringstream ss;ss << val;mMembers[key] = ss.str();}// Attempt to convert a member (the "key") to type "T"// Returns true on a successful conversion, and false on errortemplate<typename T>bool get( const string& key, T& val ) {stringstream ss( mMembers[key] );return (ss >> val);}// Alterante formtemplate<typename T>T get2( const string& key ) const {map<string, string>::const_iterator it = mMembers.find( key );if ( it == mMembers.end() )throw runtime_error( "Unable to find key [...]" );T temp;stringstream ss( it->second );if ( !(ss >> temp) )throw runtime_error( "Unable to convert member [...] to type [...]" );return temp;}private:map<string, string>mMembers;};int main() {DynamicClass bob;int intv = -1;bool boolv;string strv;bob.set( "value", int(5) );bob.get( "value", intv );cout << intv << endl;cout << bob.get2<int>( "value" ) << endl;bob.set( "value", string("Hi") );bob.get( "value", strv );cout << strv << endl;cout << bob.get2<string>( "value" ) << endl;if ( !bob.get( "value", intv ) )cout << "Error converting 'value' to type 'int'." << endl;elsecout << intv << endl;//cout << bob.get2<int>( "value" ) << endl; // throws exceptionbob.set( "value", true );bob.get( "value", boolv );cout << boolalpha << boolv << endl;cout << bob.get2<bool>( "value" ) << endl;}Output:55HiHiError converting 'value' to type 'int'.truetrue[Edited by - _fastcall on May 16, 2010 9:44:56 PM];Quote:Original post by _fastcallC++ isn't dynamic typed, therefore, you need to create this functionality yourself....or you could take the hint and change your design. In general, if there's no easy way to do something in your language it means you're doing something weird/not-idiomatic. It's a code smell. Change languages or design.;Quote:Original post by TelastynIt's a code smell. Change languages or design.Smells like a C++ backend for a scripting language ;)   ;Quote:Original post by Matthew ShockleyI am trying to make a simple thing like this:*** Source Snippet Removed ***I want something like this.Except that's not simple in C++. You're looking for a dynamically typed language.   