| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
共有 500 人关注过本帖
标题:返回Prole比它好在哪里呢
取消只看楼主 加入收藏
后卿
Rank: 4
来 自:网络
等 级:业余侠客
威 望:1
帖 子:302
专家分:295
注 册:2016-10-22
结帖率:81.71%
收藏
已结贴  问题点数:1 回复次数:0 
返回Prole比它好在哪里呢
程序代码:
typedef struct Role
{
    string Name;
    int Hp;
    int Mp;
} *Prole;
Role CreatMonster(string str, int hp, int mp)
{
    Role ghost{ str,hp,mp };
    return ghost;
}
int main()
{
    Role ghost{ CreatMonster("A",1000,1000) };
    std::cout << ghost.Name;
    system("pause");
}

请问,1.为什么这里第10行返回值时ghost,它是一个局部变量,这段代码还能运行,局部变量不是遇到括号就结束了吗?
2.这种方式返回ROLE,跟返回值Prole,有什么区别吗?返回Prole比它好在哪里呢
返回Prole
程序代码:
Prole CreatMonster(string str, int hp, int mp)
{
    Prole ghost = new Role{ str,hp,mp };
    return ghost;
}

我觉得同样是申请一块内存,在减小开销上并没有区别
2023-04-06 12:33
快速回复:返回Prole比它好在哪里呢
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013079 second(s), 9 queries.
Copyright©2004-2025, BC-CN.NET, All Rights Reserved