| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 717 人关注过本帖
标题:请教关于返回函数引用
只看楼主 加入收藏
yangyuantao
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2005-8-4
收藏
 问题点数:0 回复次数:7 
请教关于返回函数引用
比如说:
int &function(int)
像这样的返回方式是怎样的?
搜索更多相关主题的帖子: 函数 
2005-08-04 16:46
天使预备役
Rank: 2
等 级:论坛游民
威 望:3
帖 子:670
专家分:10
注 册:2004-4-6
收藏
得分:0 
就是引用呀,但要保证引用的值不会在函数结束后释放空间!

差点把你忘了...
2005-08-04 16:55
yangyuantao
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2005-8-4
收藏
得分:0 
谢谢!请指教
你能讲的再详细点吗?

2005-08-04 17:06
yangyuantao
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2005-8-4
收藏
得分:0 
请指教
#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);
}
错在哪里?

2005-08-04 17:12
Antigloss
Rank: 1
等 级:新手上路
帖 子:109
专家分:0
注 册:2004-12-30
收藏
得分:0 
b*b是一个临时的变量~return后自动毁灭~所以应该用 return b *= b;
2005-08-04 17:44
指向指针的指针
Rank: 1
等 级:新手上路
帖 子:339
专家分:0
注 册:2004-8-8
收藏
得分:0 
以下是引用Antigloss在2005-8-4 17:44:28的发言: b*b是一个临时的变量~return后自动毁灭~所以应该用 return b *= b;
不会的临时变量又被拷贝到另一个临时变量,然后又拷贝到d; 不过function(s)是哪里来的s

/sign.png" border="0" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://www./sign.png');}" onmousewheel="return imgzoom(this);" alt="" />
2005-08-05 16:13
Antigloss
Rank: 1
等 级:新手上路
帖 子:109
专家分:0
注 册:2004-12-30
收藏
得分:0 
以下是引用指向指针的指针在2005-8-5 16:13:20的发言: 不会的临时变量又被拷贝到另一个临时变量,然后又拷贝到d; 不过function(s)是哪里来的s
错!他返回的是引用!
2005-08-06 07:20
diante
Rank: 1
等 级:新手上路
帖 子:63
专家分:0
注 册:2005-7-10
收藏
得分:0 

'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.

 

[此贴子已经被作者于2005-8-6 12:43:42编辑过]

2005-08-06 12:42
快速回复:请教关于返回函数引用
数据加载中...
 
   



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

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029291 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved