#include <iostream>
#include <string>
#include <vector>
using namespace std;
/*返回数组指针或者引用的4种方法*/
/*返回数组指针,方法1,类型别名*/
using arryS = string[10];
arryS& func1();
/*返回数组指针,方法2,声明函数(较为繁琐,不易理解)*/
string(&func2())[10];
/*返回数组指针,方法3,尾置返回类型*/
auto func()->string(&)[10];
/*返回数组指针,方法4,使用decltype(已知返回数组指针指向那个数组)*/
string test[10] = { "a", "b", "c" };
decltype(test) &func();
4种声明返回数组指针函数的方法,其中第2个方法较为繁琐,不易理解,不推荐。
本文介绍了在C++中返回数组指针或引用的四种不同方法,并对比了这些方法的特点及适用场景。第一种方法利用类型别名简化语法;第二种方法虽然可行但较为繁琐;第三种采用尾置返回类型增强代码可读性;最后一种则借助decltype指定已知数组类型。
2354

被折叠的 条评论
为什么被折叠?



