Function with UnKnow Type

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2006
Posts: 61
Reputation: dev.cplusplus is an unknown quantity at this point 
Solved Threads: 0
dev.cplusplus's Avatar
dev.cplusplus dev.cplusplus is offline Offline
Junior Poster in Training

Function with UnKnow Type

 
0
  #1
Aug 12th, 2007
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:
  1. void MyMethod(char *Mychar, int MyInt, <UnKnow Type> MyUnknowType);
Is possible to write this kind of functions?
I try using the Template like
  1. template <class T>
  2. void MyMethod (char *Mychar, int MyInt, T MyUnknowType)
  3. {
  4. Do Something
  5. memberclass = MyInt;
  6. }
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Function with UnKnow Type

 
0
  #2
Aug 12th, 2007
There is no such thing as a CString in c++ me thinks.
Last edited by iamthwee; Aug 12th, 2007 at 10:18 am.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 503
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 51
Bench's Avatar
Bench Bench is offline Offline
Posting Pro

Re: Function with UnKnow Type

 
0
  #3
Aug 12th, 2007
Originally Posted by dev.cplusplus View Post
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,
If you really mean this, then templates can't help you here - the type needs to be known somewhere in your program at compile time in order for the template to be instantiated.

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?
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 1,429
Reputation: Nichito is an unknown quantity at this point 
Solved Threads: 30
Featured Poster
Nichito's Avatar
Nichito Nichito is offline Offline
Nearly a Posting Virtuoso

Re: Function with UnKnow Type

 
0
  #4
Aug 13th, 2007
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<--
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,625
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1496
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Function with UnKnow Type

 
0
  #5
Aug 13th, 2007
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:
  1. typedef enum
  2. {
  3. UNKNOWN = 0,
  4. STRING,
  5. SHORT,
  6. <etc. etc. for each data type
  7. } DATATYPE;
  8.  
  9. structure Variant
  10. {
  11. DATA_TYPE variable_type;
  12. union
  13. {
  14. unsigned char* strArray;
  15. unsigned short usVal;
  16. short sVal;
  17. int iVal;
  18. unsigned int uiVal;
  19. long lVal;
  20. unsigned long ulVal;
  21. float fVal;
  22. double dVal;
  23. };
  24. };
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC