从函数返回两个值 [英] Return two values from a Function
本文介绍了从函数返回两个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从返回类型函数中获得两个值?
请举个例子.
How can I get two values from a return type function?
Please give an example.
推荐答案
我认为,最简单的选择是使用SA的答案:
I think the easiest choice for you is to use SA''s answer:
//define a result structure
struct RESULT
{
//add as many members as you want here...
int result1;
float result2;
};
//this function returns a struct
RESULT function()
{
//declare the value you will return
RESULT returnValue;
//fill the fields
returnValue.result1 = 5;
returnValue.result2 = 12.3f;
//return the value
return returnValue;
}
要返回两个值,您可以做一些不同的事情:
1.返回一个对象.定义一个类并返回该类的实例.
2.返回一个结构.
3.返回一个数组.
4.使用参考参数(尽管这并不是最纯粹的含义).
您之前收到的答案很好.
HTH!
In order to return two values you can do several different things:
1. Return an object. Define a class and return an instance of that class.
2. Return a structure.
3. Return an array.
4. Use reference parameters (altough this is not returning in the purest meaning of it).
The answers you''ve received before are nice.
HTH!
大多数API函数都采用这种方式.
*返回错误值-检查出什么问题
*具有结构大小的参数,用于版本检查
*参数带有标志(位域)以标记有效成员
Most API functions do it in this way.
* return with error value - to check whats wrong
* parameter with struct size for version checking
* parameter with flags (bitfield) to mark the valid members
typedef enum
{
RESULTOK,
INVALIDPARAM,
BUFFERTOOSMALL,
// and so on
} MYERRORS;
typedef struct
{
unsigned int cbStruct; // ==sizeof(VARIABLE_RESULT_LIST)
enum
{
VALUE1 = 1<<0,
VALUE2 = 1<<1,
VALUE3 = 1<<2,
RESULT = 1<<2,
};
unsigned int fValdMask;
unsigned int uiValue1;
short sValue2;
TCHAR* lpValue3;
unsigned int ccValue3;
unsigned int uiAnyResult;
} VARIABLE_RESULT_LIST;
MYERRORS MyVariableFunction(VARIABLE_RESULT_LIST* pVar)
{
if(!pVar)
return INVALIDPARAM;
if(sizeof(VARIABLE_RESULT_LIST)>pVar->cbStruct)
return BUFFERTOOSMALL;
if(VARIABLE_RESULT_LIST::VALUE1 & pVar->fValdMask)
{
pVar->uiAnyResult = 42 + pVar->uiValue1;
pVar->fValdMask |= VARIABLE_RESULT_LIST::RESULT;
}
else
{
pVar->fValdMask &= ~VARIABLE_RESULT_LIST::RESULT;
}
if(VARIABLE_RESULT_LIST::VALUE3 & pVar->fValdMask)
{
if(pVar->lpValue3 && (_tcslen(__T("Hello world"))<pVar->ccValue3))
_tcscpy_s(pVar->lpValue3,pVar->ccValue3,__T("Hello world"));
else // buffer too small
pVar->fValdMask &= ~VARIABLE_RESULT_LIST::VALUE3;
}
// ignored in this version
pVar->fValdMask &= ~VARIABLE_RESULT_LIST::VALUE2;
return RESULTOK;
}
void main()
{
VARIABLE_RESULT_LIST var = { sizeof(VARIABLE_RESULT_LIST), };
TCHAR str[256];
var.fValdMask = 0;
var.uiValue1 = 23;
var.fValdMask |= VARIABLE_RESULT_LIST::VALUE1;
var.lpValue3 = str;
var.ccValue3 = sizeof(str)/sizeof(str[0]);
var.fValdMask |= VARIABLE_RESULT_LIST::VALUE3;
if(RESULTOK==MyVariableFunction(&var))
{
if(VARIABLE_RESULT_LIST::VALUE1 & var.fValdMask)
_tprintf(__T("uiValue1: %i\r\n"),var.uiValue1);
if(VARIABLE_RESULT_LIST::VALUE2 & var.fValdMask)
_tprintf(__T("sValue2: %i\r\n"),(int)var.sValue2);
if(VARIABLE_RESULT_LIST::VALUE3 & var.fValdMask)
_tprintf(__T("lpValue3: %s\r\n"),var.lpValue3);
}
}
问候.
这篇关于从函数返回两个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文