Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Zeke

Function returning different types?

This topic is 6175 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Thanks very much. I''ll give both ideas a go and see which I like more. Cheers

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!