470,872 Members | 1,743 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,872 developers. It's quick & easy.

Can I pass a type name to a function?

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;
}

Apr 9 '06 #1
11 11717
What's wrong with

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

?

Apr 9 '06 #2
A template function ?

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

Apr 9 '06 #3
A template function ?

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

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

Apr 9 '06 #5
Guch Wu wrote:
The problem is that the type is dynamic. I can't assure it when
compiling.
I want to create a object dynamicly.

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

What's wrong with

<template typename X>
void fn( X x )
{
x.somthing();
}

--
Ian Collins.
Apr 9 '06 #6
In this way, the type must be sure at compile time.

Apr 9 '06 #7
Guch Wu wrote:
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.

--
Ian Collins.
Apr 9 '06 #8
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
Apr 9 '06 #9
You could use derivation as Ian Collins suggested. Also, you can use
use typeid operator and switch on the information returned by typeid --
if that solves your purpose.

http://www.cplusplus.com/doc/tutorial/typecasting.html

Apr 9 '06 #10
>> What's wrong with
<template typename X>
void fn( X x )
{
x.somthing();
}
Guch Wu wrote: 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.
Apr 10 '06 #11
You need to use the "object factory" pattern. Modern C++ Design book
has a good writeup on it. Loki library that accompanies the book
(available on sourceforge) contains the necessary implementation
(http://cvs.sourceforge.net/viewcvs.p...6&view=markup).

Apr 10 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Sen | last post: by
7 posts views Thread by Tom Plunket | last post: by
6 posts views Thread by DMUM via AccessMonster.com | last post: by
3 posts views Thread by Ronald S. Cook | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.