关于引用的问题
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}
2010-05-23 20:54
2010-05-24 12:06
2010-05-25 10:01
2010-05-28 17:32
程序代码:#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