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:

void MyMethod(char *Mychar, int MyInt, <UnKnow Type> MyUnknowType);

Is possible to write this kind of functions?
I try using the Template like

template <class T>
void MyMethod (char *Mychar, int MyInt, T MyUnknowType) 
{
  Do Something
  memberclass = MyInt;
}

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

Recommended Answers

All 4 Replies

Member Avatar for iamthwee

There is no such thing as a CString in c++ me thinks.

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?

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...

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:

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;
    };
};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.