Function returning different types?

Started by
4 comments, last by Zeke 22 years, 9 months ago
Is it possible for 1 function to return different variable types? For example if I pass in flags that depict 16 bit or lower it will return a WORD but if the flags depict 24bit it will return a RGBTRIPLE. I think I read somewhere that this is possible but could anyone please enlighten me on this? Thanks for any help
Just my thoughts take them as you will. "People spend too much time thinking about the past, whatever else it is, its gone"-Mel Gibson, Man Without A Face
Advertisement
As far as i can see there are only two way of doing this,

1) Set the return type to void, and then typecast the returned data to the correct type (you could check this by using sizeof() function on the returned data, assuming that they are both of different sizes).

2) Pass in two variables (by reference), one a DWORD and the other an RGBTRIPLE, and set the corresponding variable to return, (the other should be returned as a NULL).

Hope this helps,

BloatWare

Edited by - BloatWare on July 24, 2001 7:35:19 AM
Thanks very much. I''ll give both ideas a go and see which I like more. Cheers
Just my thoughts take them as you will. "People spend too much time thinking about the past, whatever else it is, its gone"-Mel Gibson, Man Without A Face
The simple answer is, no. In C++, you cannot overload functions based on return type. So you would need to do one of the couple suggestions that Bloatware suggested (although for #1, you cannot cast to/from void, but rather from void*, and sizeof won''t work properly with that, and it''s not typesafe, yadda yadda yadda). Having functions like this:

  void foo(int flags, WORD& result);void foo(int flags, RGBTRIPLE& result);  


Another way is using templates, like this:
  template <typename T> T foo(int flags);  WORD result = foo<WORD>(flags);RGBTRIPLE rgb = foo<RGBTRIPLE>(flags);  


However, in all cases (even with the void*, since you have to know what to typecast to) the caller must provide the information on what the return type is, and cannot choose the return type based solely on the flags parameter.

Note that it''s not impossible to change return types based solely on the flags param; but it will require much more structural programming akin to writing a new language.

---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!
Thanks mossmoss. I had to change my code quite a bit and found that I didnt need it to return different types after all.

Cheers
Just my thoughts take them as you will. "People spend too much time thinking about the past, whatever else it is, its gone"-Mel Gibson, Man Without A Face
At looking glass, they did this:

enum eParamType {
kMT_Int,
kMT_String,
kMT_Vector,
kMT_Float,
kMT_Undef
};

struct sMultiParm {
union {
int i;
float f;
char* str;
mxs_vector* pvec;
}
eParamType type;
};

void DoSomething (sMultiParm* pRet);

They also had a cMultiParm that would let you
{
int iObj;
...
DoSomething (cMultiParm(iObj));
}

or more precisely, in your case
cMultiParm parm;
DoSomething (parm);
iObj = parm;

Inside, they would have it like this
cMultiParm::operator sMultiParm* () {
return this;
}
cMultiParm::operator int () {
ASSERT(type == kMT_Int);
return i;
}
--
(cMultiParm derived from sMultiParm)


Just FYI.

Edited by - GayleSaver on July 27, 2001 2:25:23 AM
VK

This topic is closed to new replies.

Advertisement