char CwjbString::operator [] (const int n)不考虑const的缘故,没引用,就不能对下标值进行修改。
如果cin>>a>>b,要加引用时必须的。因为原始的只能读取一个。如果是cin>>a.则影响不大
如果cin>>a>>b,要加引用时必须的。因为原始的只能读取一个。如果是cin>>a.则影响不大
程序代码:#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;
}