Can I pass a type name to a function?

G

Guch Wu

I want to pass a type name to a function.
Then in this function, create a instance of this type according to the
type name.

Can I do this?

void test(typename X)
{
X x;
do sth with x;
return;
}
 
R

Robert J. Hansen

What's wrong with

template <class X>
void test() {
X x;
x.do_something();
}

?
 
R

Rani Kamale

A template function ?

template <typename X>
void test(void)
{
X x;
// do stuff
return;
}
 
R

Rani Kamale

A template function ?

template <typename X>
void test(void)
{
X x;
// do stuff
return;
}
 
G

Guch Wu

The problem is that the type is dynamic. I can't assure it when
compiling.
I want to create a object dynamicly.
 
I

Ian Collins

Guch said:
In this way, the type must be sure at compile time.
In what way?

Please quote some context in your replies. See
<http://cfaj.freeshell.org/google/>.

Your answer is no, C++ is a typed language, so you can't have a function
parameter without a type.

Your only option is to have all the types you wish to pass derived form
a common base and make the function parameter a pointer or reference to
the base class.
 
T

Tomás

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::eek:stringstream );
}
}

#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::eek:stringstream\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
 
S

Salt_Peter

What's wrong with
Guch said:
In this way, the type must be sure at compile time.

That same template can generate as many functions as the compilation
requires (small code - big dividends). At compile time - type X is not
an unknown type or a set of unknown types. At runtime, type X can be any
derivative thereof too. In fact the only requirement above is that any
substitute for type X must have a somthing().

What we may not know is which version of that template function is
called until runtime.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top