找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 1539|回复: 7

请教关于返回函数引用

[复制链接]
发表于 2009-10-31 01:51:28 | 显示全部楼层 |阅读模式 IP:江苏扬州
比如说:
int &function(int)
像这样的返回方式是怎样的?
发表于 2009-10-31 01:51:29 | 显示全部楼层 IP:江苏扬州
就是引用呀,但要保证引用的值不会在函数结束后释放空间!
回复

使用道具 举报

发表于 2009-10-31 01:51:29 | 显示全部楼层 IP:江苏扬州
你能讲的再详细点吗?
回复

使用道具 举报

发表于 2009-10-31 01:51:30 | 显示全部楼层 IP:江苏扬州
#include<iostream.h>
int & fiction(int&);
void main()
{
  int a = 4;
  int d = fiction(s);
  cout << a << '\t' << d << endl;
}
int & fiction(int &b)
{
  return (b*b);
}
错在哪里?
回复

使用道具 举报

发表于 2009-10-31 01:51:31 | 显示全部楼层 IP:江苏扬州
b*b是一个临时的变量~return后自动毁灭~所以应该用 return b *= b;
回复

使用道具 举报

发表于 2009-10-31 01:51:32 | 显示全部楼层 IP:江苏扬州
以下是引用Antigloss在2005-8-4 17:44:28的发言: b*b是一个临时的变量~return后自动毁灭~所以应该用 return b *= b;
不会的临时变量又被拷贝到另一个临时变量,然后又拷贝到d; 不过function(s)是哪里来的s
回复

使用道具 举报

发表于 2009-10-31 01:51:33 | 显示全部楼层 IP:江苏扬州
以下是引用指向指针的指针在2005-8-5 16:13:20的发言: 不会的临时变量又被拷贝到另一个临时变量,然后又拷贝到d; 不过function(s)是哪里来的s
错!他返回的是引用!
回复

使用道具 举报

发表于 2009-10-31 01:51:34 | 显示全部楼层 IP:江苏扬州
'conversion' : cannot convert from 'type1' to 'type2'
The compiler cannot cast from 'type1' to 'type2'.
The C2440 errors on lines 15 and 16 of this sample code will be qualified with the Incompatible calling conventions for UDT return value message. (A UDT is a user-defined type, such as a class, struct, or union.) These type incompatibility errors are caused when the calling convention of a UDT specified in the return type of a forward declaration conflicts with the actual calling convention of the UDT and when a function pointer is involved.
In the example, we first have forward declarations for a struct and for a function that returns the struct; the compiler assumes that the struct uses the C++ calling convention. Then we have the struct definition, which, by default, uses the C calling convention. Since the compiler does not know the calling convention of the struct until it finishes reading the entire struct, the calling convention for the struct in the return type of get_c2 is also assumed to be C++.
The struct is followed by another function declaration that returns the struct, but at this point, the compiler knows that the struct's calling convention is C++. Similarly, the function pointer, which returns the struct, is defined after the struct definition so the compiler knows that the struct uses the C++ calling convention.
The errors occur because the function pointers, which expect to return a type using the C calling convention, are assigned to functions that expect to return a type using the C++ calling convention.
To resolve C2440 because of incompatible calling conventions, declare functions that return a UDT after the UDT definition.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-29 15:19 , Processed in 0.188000 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表