473,394 Members | 1,887 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 11938
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Stephen Williams | last post by:
Hey i've got bunch of arrays of tick boxes, each array contains somewhere between 5 and 20. What I want to do is write a function that returns the captions of every ticked tick box in an array...
4
by: Sen | last post by:
This is the Javascript function: function auto_submit(form_name, value) { document.form_name.elements.click(); } This is the XHTML: <select name="category"...
1
by: NorrYtt | last post by:
I am having a problem with passing a function as a parameter to my DLL so it can call it back with updates. The DLL is written in LabWindows CVI with some #ifdef 'C'-style wrappings. The...
7
by: Tom Plunket | last post by:
I'd like to figure out if a given parameter is a function or not. E.g. <type 'int'> True implies: .... pass
6
by: DMUM via AccessMonster.com | last post by:
Hello I am trying to pass the name of my subform to a function/sub but I can't seem to get it to work. I am using an autokey function (ctrl E) to unlock text boxes on a subform. I have a few...
3
by: Ronald S. Cook | last post by:
I want to something as simple as: UserControl uctTemp; But the type will be passed in to the function (which will be an existing user control like "uctMyUserControl1") So, how can I pass in...
11
by: John Brown | last post by:
Hi there, Does anyone know how to extract the full type name ("namespace.type") from an assembly qualified name. Thanks.
6
by: Deckarep | last post by:
I want to be able to pass in a function a string say: "TextBox" Then I need a way to convert that string representation into a Type object so i can search through some controls and check their...
1
by: Nitin | last post by:
Hi, Can I devise function pointer type from function name by using template programming? e.g. I have function: exertn “C” void xmlFreeDoc(xmlDocPtr cur); I want something like following:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.