I want to write a cpp function something similar to this, where outVal should accept data of the corresponding type returned from the respective functions in each case.
void ReadFieldByType(char fieldType, string content, string& outStr, **unknown**& outVal){
unsigned int ifield;
char cField;
string sField;
double dField;
switch (fieldType)
{
case 'c':
ReadChar(content, content, cField);
outVal = cField;
break;
case 'd':
ReadInteger(content, content, ifield);
outVal = ifield;
break;
case 'f':
ReadDouble(content, content, dField);
outVal = dField;
break;
case 's':
ReadString(content, content, sField);
outVal = sField;
break;
default:
break;
}
outStr = content;}
I don’t know how to set datatype for outVal. Can this be improved or is there any other choice to accomplish this task?
You could probably do it with templates and the
typeidoperator:Although I have to admit that I don’t like the solution.