C++函数返回多个参数
2015-09-02 10:16
344 查看
如果一个函数需要返回多个参数,可以采用以下两种方法:
第一种方法是将返回值作为写参数。#include <iostream>
#include <string>
using namespace std;
void fun(int &a, char &b, string &c)
{
a = 1;
b = 'b';
c = "test";
}
int main()
{
int a;
char b;
string c;
fun(a,b,c);
cout << a << " " << b << " " << c << endl;
}第二种方法是定义一个结构,返回指向该结构的指针。
#include <iostream>
#include <string>
using namespace std;
struct result
{
int a;
char b;
string c;
};
result * fun()
{
result *test=new result;
test->a = 1;
test->b = 'b';
test->c = "test";
return test;
}
int main()
{
result *test;
test=fun();
cout << test->a << " " << test->b << " " << test->c << endl;
delete test;
return 1;
}
第一种方法是将返回值作为写参数。#include <iostream>
#include <string>
using namespace std;
void fun(int &a, char &b, string &c)
{
a = 1;
b = 'b';
c = "test";
}
int main()
{
int a;
char b;
string c;
fun(a,b,c);
cout << a << " " << b << " " << c << endl;
}第二种方法是定义一个结构,返回指向该结构的指针。
#include <iostream>
#include <string>
using namespace std;
struct result
{
int a;
char b;
string c;
};
result * fun()
{
result *test=new result;
test->a = 1;
test->b = 'b';
test->c = "test";
return test;
}
int main()
{
result *test;
test=fun();
cout << test->a << " " << test->b << " " << test->c << endl;
delete test;
return 1;
}
相关文章推荐
- C++中的inline函数
- C++基础
- c++ 扩栈
- Effective C++ 改善程序与设计的55个具体做法 读书笔记
- C++中 模板Template的使用
- std空间和全局命名空间
- 详解C语言中symlink()函数和readlink()函数的使用
- C++函数指针
- C语言复习 -- 结构体指针与自增运算符
- 简单谈谈C++ 中指针与引用
- C语言中的结构体与C++中的类异同
- CALLBACK--_stdcall(PASCAL调用方式),VS默认是C语言调用方式即__cdecl
- C/C++中static关键字不同的含义详解
- C语言的实用工具库
- C++ Primer : 第十三章 : 拷贝控制之拷贝、赋值与销毁
- 开始使用虚幻4并创建一个简单的c++类
- C语言编写控制台下PE分析工具(四)
- C++成员函数的 重载、隐藏、覆盖分析
- c/c++使用gsoap调用wcf发布的webservice
- c/c++使用gsoap发布和调用webservice