最近工作中遇到了这样的几个函数:
GetTimeToElapseForTimer(int channel,CTime t,int* curWay,
int* CurTimeSectionCount)
{
...........
GetCurTimeSection(&bookstart,&bookend,channel,
temptime,weekday,&daycount,&recordway,CurTimeSectionCount);
}
GetCurTimeSection(int *bookstart, int *bookend,int channel,
int time,int day,int* daycount,int* recordway,
int* CurTimeSectionCount)
{
.......
GetFirstSectionOfTheLatestDay(bookstart,bookend,channel,day);
}
/*
GetFirstSectionOfTheLatestDay(int * bookstart,
int * bookend,
int channel, int day)*/ 我想问的是,向这样的函数:
要求有多个返回值的函数,这样写合适么?
有没有好的办法解决?
有的朋友说,用struct或则Array,我想这是一个办法,
struct MyStruct{
int bookstart;
int bookend;
...
}
或则int MyArray[100];
MyArray[0]=bookstart;
.......
但是数据之间没有联系的话,这样写合适么?我的理解是不合适
偶是初学者,请大家帮忙哈,谢谢!!
GetTimeToElapseForTimer(int channel,CTime t,int* curWay,
int* CurTimeSectionCount)
{
...........
GetCurTimeSection(&bookstart,&bookend,channel,
temptime,weekday,&daycount,&recordway,CurTimeSectionCount);
}
GetCurTimeSection(int *bookstart, int *bookend,int channel,
int time,int day,int* daycount,int* recordway,
int* CurTimeSectionCount)
{
.......
GetFirstSectionOfTheLatestDay(bookstart,bookend,channel,day);
}
/*
GetFirstSectionOfTheLatestDay(int * bookstart,
int * bookend,
int channel, int day)*/ 我想问的是,向这样的函数:
要求有多个返回值的函数,这样写合适么?
有没有好的办法解决?
有的朋友说,用struct或则Array,我想这是一个办法,
struct MyStruct{
int bookstart;
int bookend;
...
}
或则int MyArray[100];
MyArray[0]=bookstart;
.......
但是数据之间没有联系的话,这样写合适么?我的理解是不合适
偶是初学者,请大家帮忙哈,谢谢!!
像函数GetCurTimeSection()
参数倒是可以考虑用结构,是预定信息吧
int bookstart;
int bookend;
...
};
你可以这样:
int myfun(MyStruct*);
或者
int myfun(MyStruct&);你还可以这样:MyStruct myfun();//让你的函数直接返回这样的一个结构.我个人认为这样的情况下使用数组不是太合适, 就象你说的那样, 数据之间的关系体现不出来了, 程序的可读性大大降低了. (个人见解)
不过象你这个应用,我觉得还是用结构好,最好定义一个数据接口.操作接口可以使程序更通用一些.
struct aaa
{
int b;
int c;
int d;
};
aaa foo(...)
{
}
建议你用结构体和数组都比较好.考虑效率的问题,你可以把结果通过引用或指针传回来,而不是通过用返回值的方式.