| | |
Function with UnKnow Type
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Hi to all, I hope uyou can help with the following situation i have:
I have to write a function that receives a parameter, that is unknow until runtime, meaning one of the parameters could be:
CString, int, double, bool or something else
The function receive additional parameters that are know and one that is unknow, for example:
Is possible to write this kind of functions?
I try using the Template like
The problem is that the function has to use a data member of my class and the (template)method dot reconized that variable member.
And I don't want to rewrite all the class like template only for one function
Thanks
I have to write a function that receives a parameter, that is unknow until runtime, meaning one of the parameters could be:
CString, int, double, bool or something else
The function receive additional parameters that are know and one that is unknow, for example:
C++ Syntax (Toggle Plain Text)
void MyMethod(char *Mychar, int MyInt, <UnKnow Type> MyUnknowType);
I try using the Template like
C++ Syntax (Toggle Plain Text)
template <class T> void MyMethod (char *Mychar, int MyInt, T MyUnknowType) { Do Something memberclass = MyInt; }
And I don't want to rewrite all the class like template only for one function
Thanks
•
•
•
•
Hi to all, I hope uyou can help with the following situation i have:
I have to write a function that receives a parameter, that is unknow until runtime, meaning one of the parameters could be:
CString, int, double, bool or something else
The function receive additional parameters that are know and one that is unknow,
You could think of a templated function as a "meta" function, which doesn't exist unless it is somehow given full type information to fill-in the blanks when the function is used. The compiler automatically generates these variations of the template as seperate functions at compile time.
Perhaps you could shed a little more light on where this function appears in your program, what you want it to do, and how it fits into your design. Also, how does the type affect what the function does?
Last edited by Bench; Aug 12th, 2007 at 6:07 pm.
¿umop apisdn upside down? why don't you initialize your unknown variable as a string, and depending on its value you convert it to int, double, boolean, or leave it as a string... you may use atoi() to compare numbers and strings such as "true" and "false" to compare boolean... if there is a dot in middle of the string and the rest of the characters are numbers, then it is a double... and if not, it is a string...
-->sometimes i wanna take my toaster in a bath<-- you could also make it a structure that contains a union, something like the VARIANT structure that is used frequently by MS-Windows compilers such as VC++ and VB. It looks like this:
C++ Syntax (Toggle Plain Text)
typedef enum { UNKNOWN = 0, STRING, SHORT, <etc. etc. for each data type } DATATYPE; structure Variant { DATA_TYPE variable_type; union { unsigned char* strArray; unsigned short usVal; short sVal; int iVal; unsigned int uiVal; long lVal; unsigned long ulVal; float fVal; double dVal; }; };
Last edited by Ancient Dragon; Aug 13th, 2007 at 10:05 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- help with creating and calling a function (C++)
- Deleting Characters Function (C)
- what is pointer to function ??? (C)
- Help me recognize function? (C)
- Problem with string variable in void prnt function (C++)
- having a small problem with the gets function (C)
- A puzzl about function (C)
- Is ifs is a member function of ifstream class (C++)
Other Threads in the C++ Forum
- Previous Thread: Reading and writing from files works selectively...
- Next Thread: problems with reading in file
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






