943,936 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 968
  • C++ RSS
Aug 12th, 2007
0

Function with UnKnow Type

Expand 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, for example:
C++ Syntax (Toggle Plain Text)
  1. void MyMethod(char *Mychar, int MyInt, <UnKnow Type> MyUnknowType);
Is possible to write this kind of functions?
I try using the Template like
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
dev.cplusplus is offline Offline
61 posts
since Jun 2006
Aug 12th, 2007
0

Re: Function with UnKnow Type

There is no such thing as a CString in c++ me thinks.
Last edited by iamthwee; Aug 12th, 2007 at 10:18 am.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Aug 12th, 2007
0

Re: Function with UnKnow Type

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.
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006
Aug 13th, 2007
0

Re: Function with UnKnow Type

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...
Featured Poster
Reputation Points: 424
Solved Threads: 57
Posting Virtuoso
Nichito is offline Offline
1,594 posts
since Mar 2007
Aug 13th, 2007
0

Re: Function with UnKnow Type

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)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Reading and writing from files works selectively...
Next Thread in C++ Forum Timeline: problems with reading in file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC