Guch Wu posted:
The problem is that the type is dynamic. I can't assure it when
compiling.
I want to create a object dynamicly.
An exception maybe?
#include <memory>
enum Type { String = 1, VectorInt, Double, Exception, OStringStream };
void CreateObject( Type type )
{
switch (Type)
{
case String: throw std::auto_ptr( new std::string );
case VectorInt: throw std::auto_ptr( new std::vector<int> );
case Double: throw std::auto_ptr( new std::double );
case Exception: throw std::auto_ptr( new std::exception );
case OStringStream: throw std::auto_ptr( new std::ostringstream );
}
}
#include <iostream>
using std::cout; using std::cin; using std::endl;
int main()
{
cout << "\nWhich type would you like to create an object of?\n\n"
"1) std::string\n"
"2) std::vector<int>\n"
"3) double\n"
"4) std::exception\n"
"5) std::ostringstream\n\n"
"Enter your choice: ";
unsigned char choice = 0;
cin >> choice;
if ( choice < 1 || choice > 5 ) return -1;
try {
CreateObject( choice );
}
catch ( std::auto_ptr<std::string> p ) { ...
catch ( std::auto_ptr<std::vector<int> > p ) { ...
}
Something tells me though that you don't actuall need this.
-Tomás