以前编写python习惯多参数返回,编一个c++程序发现一直数不对,最后发现是子函数参数返回没法一次多个的问题。
解决:
结构体法:
#include <iostream>
#include <string>
using namespace std;
struct result
{
int a;char b;
};
result * testone()
{
result *test=new result;
test->a = 1;
test->b = 't';
return test;
}
int main()
{
result *test;
test=testone();
cout << test->a << " " << test->b << endl;
return 0;
}
地址法:
#include <iostream>
#include <string>
using namespace std;
void test(int &a, int &b)
{
a = 1;b = 3;
}
int main()
{
int a;int b;
test(a,b);
cout << a << " " << b << endl;
return 0;
}