C++关于引用作为函数的用法

2020-01-06 20:04:02于海丽

引用作为函数参数


#include <stdio.h>
int add(int& a, int& b)
{
 return a + b; 
}
int main()
{
 int a = 1, b = 2;
 printf("%dn", add(a, b));
 return 1;
}

引用作为函数的返回值


#include <stdio.h>
#include <string.h>
struct Student
{
 char name[32];
 int age;
};
Student stu;
Student& fun()
{
 strcpy(stu.name, "aaa");
 stu.age = 30;
 return stu;
}
int main()
{
 Student& stu = fun();
 printf("name = %s, age = %dn", stu.name, stu.age);
 return 1;
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对ASPKU的支持。


注:相关教程知识阅读请移步到C++教程频道。