473,763 Members | 6,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11971
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::ostringstr eam );
}
}

#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::ostringstr eam\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<s td::string> p ) { ...
catch ( std::auto_ptr<s td::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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2961
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 as a string. I want to be able to pass to the function the array name, and the lower and upper limits of the array. But I have no idea how to pass the control name (array name).
4
18926
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" onchange="auto_submit(this.form.name,'Select')"> So, when choosing an option from the select list, the Select button is clicked and the form is submitted. Somehow the function only works when
1
2648
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 application that calls the DLL is in MS VC++ .NET. It basically goes like this: -There's a DLL initialization routine that the application will call. This routine takes a function as a parameter. This is normally a (void *) but I've tried a couple...
7
1490
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
5975
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 forms in the database that will use this function, so I need to be able to tell the code which form to unlock. What I have is as follows: Public Function akeyEdit()
3
2140
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 the string "uctMyUserControl1" and then somehow dim an instance of it?
11
15148
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
1977
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 type. Then I can do my check: If ( control is type) { //continue } I'm doing this inside of a recursive function which goes through the ControlTree.
1
1565
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: typeof(xmlFreeDoc) functionpointer = xmlFreeDoc; i.e value to type conversion
0
9563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10144
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9937
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8821
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7366
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.