函数返回数组指针!

cutter_point 2014-05-25 10:12:41
编写一个函数声明,使其返回数组的引用并且该数组包含10个string对象,使用尾置返回类型、decltype,还有类型别名!三种方式实现
...全文
228 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
cutter_point 2014-07-01
  • 打赏
  • 举报
回复
引用
#1
是大多数
cutter_point 2014-05-25
  • 打赏
  • 举报
回复
额 这个我倒是会一点 我是那个函数里面的返回不会啊 我用return &s2的时候老错
/******************************************************/
//普通返回类型
string (*s1)[10];

string (*buf1())[10]
{
    cout<<"This is s1!"<<endl;
    return s1;
}
/******************************************************/
/*
//类型别名?这个我还写不出来,我也不知道为什么不行,我正在求助中ing...
typedef string s2[10];

using s2=string[10];

s2 *buf2()
{
    cout<<"This is s2!"<<endl;
    return &s2;
}
*/
/******************************************************/
//尾置返回类型
string s3[10];

auto buf3() -> string (*)[10]
{
    cout<<"This is s3!"<<endl;
    return &s3;
}
/******************************************************/
//decltype
string s4[10];

decltype(s4) *buf4()
{
    cout<<"This is s4!"<<endl;
    return &s4;
}
/******************************************************/

int main()
{
    buf1();
    buf3();
    buf4();

    return 0;
}
cutter_point 2014-05-25
  • 打赏
  • 举报
回复
thank you 各位啦 我先看看再说
mujiok2003 2014-05-25
  • 打赏
  • 举报
回复
std::array<std::string, 10> & foo(...);
ri_aje 2014-05-25
  • 打赏
  • 举报
回复

std::string a [10];
auto f0 () -> std::string (&) [10];
auto f1 () -> decltype((a));
cutter_point 2014-05-25
  • 打赏
  • 举报
回复
I just want to ask a question!!
cutter_point 2014-05-25
  • 打赏
  • 举报
回复
求解 我是真写不出来,重昨天搞到现在,没办法╮(╯_╰)╭
ri_aje 2014-05-25
  • 打赏
  • 举报
回复
引用 楼主 cutter_point 的回复:
返回数组的引用[/img]
doing so is asking for trouble.
cutter_point 2014-05-25
  • 打赏
  • 举报
回复
/* //类型别名 typedef string s2[10]; using s2=string[10]; s2 *buf2() { cout<<"This is s2!"<<endl; return &s2; } */
cutter_point 2014-05-25
  • 打赏
  • 举报
回复
//类型别名 typedef string s2[10]; using s2=string[10]; auto *buf2() { cout<<"This is s2!"<<endl; return &s2; } 为毛是错的

65,208

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧