| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 966 人关注过本帖
标题:关于引用的问题
取消只看楼主 加入收藏
安普留香
Rank: 2
等 级:论坛游民
帖 子:77
专家分:62
注 册:2010-5-17
结帖率:75%
收藏
 问题点数:0 回复次数:4 
关于引用的问题
1。如果int& f()   int x=f(),那么x与f()的返回值共用地址对吗??
2。cin和cout不是变量怎么可以作为参数被传递???
   对于操作符重载的函数中,比如>>重载。istream& operate>>(istream& istr,int& x){......return istr}
   这样不行吗??->  istream operate>>(istream& istr,int& x){......return istr}
搜索更多相关主题的帖子: operate return 
2010-05-23 20:54
安普留香
Rank: 2
等 级:论坛游民
帖 子:77
专家分:62
注 册:2010-5-17
收藏
得分:0 
1。int x=f()对于int& f()h和int f()是一样的了??
2。istream& operate>>(istream& istr,int& x){......return istr}
   istream operate>>(istream& istr,int& x){......return istr}
   可否具体说下2者的区别,书上对返回值为引用的函数说的不够详细
2010-05-24 12:06
安普留香
Rank: 2
等 级:论坛游民
帖 子:77
专家分:62
注 册:2010-5-17
收藏
得分:0 
下面的程序错在哪??出现一堆错误
#include <iostream>
using namespace std;
istream& operator>>(istream& istr,int& b)
int main()
{
int x,y;
cin>>x>>y;
cout<<x<<" "<<y<<endl;
return 0;
}
istream& operator>>(istream& istr,int& b)
{
cin>>b;
return istr;
}
2010-05-25 10:01
安普留香
Rank: 2
等 级:论坛游民
帖 子:77
专家分:62
注 册:2010-5-17
收藏
得分:0 
其实我就是对重载运算符返回引用不太清楚
你的例子是说第一个返回的m_pString[n]可以作为左值被修改,第二个不行。对吧??
2010-05-28 17:32
安普留香
Rank: 2
等 级:论坛游民
帖 子:77
专家分:62
注 册:2010-5-17
收藏
得分:0 
重载赋值符=时,返回值没有必要用引用吧?!这里的代码是我从书上弄来的,书上说要返回引用,我把引用去了结果也一样
程序代码:
#include <iostream>
using namespace std;
class cat
{
public:
cat();
int getage() const {return *itsage;}
int getweight() const{return *itsweight;}
void setage(int age){*itsage=age;}
cat operator=(const cat&);
private:
int *itsage;
int* itsweight;
};
cat::cat()
{
itsage=new int;
itsweight=new int;
*itsage=5;
*itsweight=9;
}
cat cat::operator=(const cat & rhs)
{
if(this==&rhs)
return *this;
*itsage=rhs.getage();
*itsweight=rhs.getweight();
return *this;
}
int main()
{
cat frisky,newsky;
cout<<"frisky's age:"<<frisky.getage()<<endl;
cout<<"setting frisky to 6...\n";
frisky.setage(6);
cat whiskers;
cout<<"whiskers'age:"<<whiskers.getage()<<endl;
cout<<"copying frisky to whiskers..\n";
newsky=whiskers=frisky;
cout<<"whiskers' age:"<<whiskers.getage()<<endl;
cout<<"newsky's age"<<newsky.getage()<<endl;
system("pause");
return 0;
}


2010-05-28 20:23
快速回复:关于引用的问题
数据加载中...
 
   



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

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