How to return different types ?

Pages: 12
I'd like to have a function to return a different type depending if the result can be a double a float an int or a long.
Is it possible ?
Thanks
Yes, two possibilities are:

Function Overloading http://www.cplusplus.com/doc/tutorial/functions2/#function_overload
and
Templates http://www.cplusplus.com/doc/tutorial/templates/

The difference being how creative you are.
mmmm ...

I'm searching something like:


type_to_return Myfunction( a_type value)
if (blablah)
return float;
if (another condition)
return double;

.....
Thanks
Templates. Take a look at the tutorial I linked, you could not have asked for a more perfect implementation.
Mind you - neither templates or function overloading can work by return type alone - he will have to also use different parameter types to distinguish the functions.
Last edited on
@ guestgulkan: I'm sorry, I don't follow you. Do you mean when he goes to initialize the functions?
He means you can't have something like this:

1
2
3
4
template <class T>
T myfunc(int x) {
    //blah blah
}


and just call it. You must either specify the type or have some parameter from which it can be inferred.
I just meant that when overloading functions you have to change the type/quantity of function parameters - not just the return type.

So you can't have an overloaded set of functions like this:

1
2
3
//error overloading just by return type is illegal
int function_one (int);
string function_one (int);


which I am second guessing he will end up doing if he is not careful.
Last edited on
Ah, I gotcha now. That's important to keep in mind.
@tonnot
Whatever it is you are trying to do, you are doing it wrong.

What is it you are trying to accomplish? We can help you do it correctly.
I want to have a function to determine what number can be a string .
A int, a long, a float, a double, etc.
And I'd want to return the type .
Now I have a function like :
1
2
bool try_ifint(string_value_to_convert)
if (can_convert_to_int ) my_int=value(string)

1
2
3
bool try_if_double(string_value_to_convert)
if (can_convert_to_double) my_double=value(string)
....

And at code:
If try_if_int
if try_if_code
...
But I think it is possible to do something better
Thanks
Last edited on
How about you:
1. determine the type in the string in the function
2. return an arbitrary integer into main from that function: ie. int = 1, float = 2, double = 3, etc..
3. then, switch the return variable and do the correct conversion according to what type it is.

This isn't very efficient and advisable I bet (I am a beginner too). Alternatively you could create a struct containing a variable for each type:
1
2
3
4
5
struct types {
int int_num;
float float_num;
//etc..
};


You first initialize an object of this struct by setting all variables to zero. Then you pass this struct by reference to the function that determines the type in the string and does the conversion. If the string represents an int, you set the int_num variable in the struct to the result of the string conversion to int. Likewise you determine the type in the string, do the conversion and go save the result into the correct variable of the object.

1
2
3
4
5
6
void function_converting_string(types &types_object, string str){
   //determine what type contained in string
   //do conversion
   // save result in correct variable in the types_object
   //example - if result is int do: types_object.int_num = str_to_int_conversion_result;
}


Please do correct me if any of these suggestions are completely wrong. I am sure they aren't really efficient, but as I said, I'm still a beginner! :)

Beavey
It can be run, but I have to write a conditional sentence and I would not want . I thought that 'powerful' c++ could do it.....
I cant define a void function and return a value, isn't? ( I think this question surplus ....)
Thanks
Unfortunately tonnot , there is a lot of ambiguity here.
In your one of your previous posts you said this:
I want to have a function to determine what number can be a string .
A int, a long, a float, a double, etc.


So lets consider if you have a string "5.25" - this can be converted to :

1. integer (with loss of precision)
2. long (with loss of precision)
3. float
4. double

in the same manner - the string "55" is convertible to:
1. integer
2. long
3. float
4 . double

So you see, it isn't an issue of c++ powerfulness - but more the nature of what you
are trying to do.
It seems you are trying to do something an interpreter (like Python or Tcl) would do.
It might be best to simply keep the object as a string until it is needed as a specific type of number.
Only when it is needed do you attempt to convert it, and deal with the error at that time.

Does this help?
A string can be thought of as a dynamic array of chars, all numerical elements (numbers, symbols etc.) have valid char values. So the answer to "I want to have a function to determine what number can be a string " is ALL OF THEM. Your function has no value because in C++ the question is irrelevant.

tonot wrote:
I cant define a void function and return a value isn't?

No, because you don't have to.

Duoas had it right from the beginning.
Duoas wrote:
Whatever it is you are trying to do, you are doing it wrong.
function templates are technically a set of overloaded functions, its just that you dont have to type out a ton of stuff right?
Yes. A template is a plan or form that the compiler can use to generate functions. So instead of writing:

1
2
3
int    incr( int&    a, int    b = 1 ) { return a += b; }
double incr( double& a, double b = 1 ) { return a += b; }
// et cetera, for every type imaginable 

You can write just:

1
2
template <typename T>
T incr( T& a, T b = 1 ) { return a += b; }

and the compiler will generate the code for the function for only those types that are needed (that is, that are used).
When I said :
"I want to have a function to determine what number can be a string .
A int, a long, a float, a double, etc."
i'd like to say " the best number"
for example : 3.14 can be a float, 3.15478788787 must have a double.

But I think that it is impossible to have a function that return a 'template' type ?
If yes, how can I define it ?

1
2
3
4
TT Myfunction (std::string value) {
    if  .....   return double  
    if  .....   return float  
   ....}


Thanks to everybody
You are missing the point.

You must have a specific type in use when you compile your program. The type of a thing is not changeable when you run your program.

All a template does is provide a cookie-cutter method for the compiler to generate functions at compile time.
Pages: 12