Function that will return string or double

Hello,
I was wondering if it is possible to have a function that can return a string or a double. Something like:

function getE(int i)
{
string ss="tst";
if(i==0) return 15.2;
else if(i==1) return ss;
}

I've "solved" the problem using a function that will return void, and sending a pointer to the result in the function, something like:

void function(int i, string &ss)

However I wonder if there is a more elegant solution using templates for example ?

Thanks,

GO
You can create the function as a double, like: double myfunct(args)

I'm not sure about strings but you can always try.
By overloading a function with different arguments (2 different functions), you can have a varied input.

You might want to return a C string (char*). Have no idea why you want to return a string of all things, and a double of it.

I'd just return a double and if I needed to convert it to a string I have a static function in a Convert class:
Convert::ToString( double value );
Last edited on
@Krisando

I've already used overloading for implementing my function:

void test(int a, string &ss)

and

void test(int a, double &xx)

The thing is that this functions pulls data out from a class which contains strings and doubles, now I was thinking it would be useful to be able to be able to use indexes instead of the actual class variables names, for example I would be able to use:

getData(0); getData(1)

instead of:

obj.Name1; obj.Name2

I think at my data as a kind of array (or list) with mixed types components.

GO
Why exactly do you want to do this?
Topic archived. No new replies allowed.