重载operator->的说明

转载: https://blog.csdn.net/todototry/article/details/1481185#commentBox


重载operator ->目的是为了使一个类对象表现的像一个类型的指针一样
如P类对象则表现的像一个A *的作用形式
operator->的返回值有两种选择
1。返回目的类型的指针
       这时把operator->的语义运用在返回值上,也就是A *那个变量身上
       注意是“语义”,就是说逻辑上的想作的,而并非语言形式
2。返回另外一个对象的引用或者值,
      然后递归上述1的过程,直到返回目的类型的指针,或语法错
      想正常的达到目的,类似出现“指针的指针”(不太恰当的比方)的嵌套概念,PP类中可以没有A *的变量,
      但是它的operator->返回P类型的引用或者对象,
      此时,若P类型无operator ->则报语法错
                  若P类型存在operator ->则递归,直到返回值是A *的时候,将语义的运用在这个返回值上,一般                      即调用A的成员函数,获取A的共有数据成员这些语义
3。类P,类PP均需和绑定的对象创建联系,不管嵌套几层,这才是联系一个
      指针类和目的类类型的桥梁,抛开这点,两个独立的类,如何运用语义到目的类对象

class A
{
public:
    void display(){cout << "yes,ok" << endl;}
};

class P
{
public:
    P(A &ra):ptr(&ra){}
    A* operator->(){return ptr;}
private:
    A *ptr;
};

class PP
{
public:
    PP(A &ra):p(ra){}
    P operator->(){return p;}
private:
    P p;
};

int main()
{
    A a;
    P p(a);
    p -> display();

    cout << endl;

    PP pp(a);
    pp -> display();
    return 0;
}

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
将以下程序补充完整 #include <iostream> #include <fstream> #include <iomanip> using namespace std; template <typename ElemType> class myArrayList { private: int mSize; int mLen; ElemType *mpArr; public: myArrayList(int n); myArrayList(ElemType *a,int n); void show(); ElemType getMax(); //以下函数由同学完成 //void sort(); //myArrayList(myArrayList<ElemtType> &other);//拷贝构造函数 //mymyArrayList<ElemType> & operator =(mymyArrayList<ElemType> &other) }; template <typename ElemType> myArrayList<ElemType>::myArrayList(int n) { this->mSize=n; this->mLen=0; this->mpArr=new ElemType[mSize]; } template <typename ElemType> myArrayList<ElemType>::myArrayList(ElemType *a,int n) { this->mSize=n; this->mLen=n; this->mpArr=new ElemType[mSize]; for(int i=0;i<mLen;i++) mpArr[i]=a[i]; } template <typename ElemType> void myArrayList<ElemType>::show() { for(int i=0;i<mLen;i++) cout<<setw(4)<<mpArr[i]; cout<<endl; } template <typename ElemType> ElemType myArrayList<ElemType>::getMax() { ElemType max; max=mpArr[0]; for(int i=1;i<mLen;i++) if(max<mpArr[i]) max=mpArr[i]; return max; } //Student.h class Student { private: int mId; float height; int score; public: Student(int id=0,float h=0,int s=0):height(h),mId(id),score(s) { } friendbool operator <(Student &a1,Student &a2) { if(a1.height<a2.height) return true; else return false; } friend ostream &operator <<(ostream &out,Student &s) { out<<"ID:"<<s.mId<<" Height:"<<s.height<<" Socre:"<<s.score<<endl; return out; } }; //主程序 int main() { int a[]={1,2,3,5,7,9,12,8}; double b[]={1,2.5,3.6,5,7,9,12.8,8}; myArrayList <int> list1(a,8); list1.show(); cout<<"max="<<list1.getMax()<<endl; myArrayList <double> list2(b,8); list2.show(); cout<<"max="<<list2.getMax()<<endl; Student s[3]={Student(1,175,80),Student(2,178,90),Student(3,195,83)},s1; myArrayList <Student> list3(s,3); list3.show(); cout<<"max="<<list3.getMax()<<endl; } 说明: 将以下程序补充完整 //void sort(); //myArrayList(myArrayList<ElemtType> &other);//拷贝构造函数 //mymyArrayList<ElemType> operator =(mymyArrayList<ElemType> &other) //修改补充 Student类,要求按成绩排序(从高到低)
06-01
将以下程序补充完整: ``` template <typename ElemType> void myArrayList<ElemType>::sort() { for(int i=0; i<mLen-1; i++) { for(int j=0; j<mLen-i-1; j++) { if(mpArr[j] > mpArr[j+1]) { ElemType temp = mpArr[j]; mpArr[j] = mpArr[j+1]; mpArr[j+1] = temp; } } } } template <typename ElemType> myArrayList<ElemType>::myArrayList(myArrayList<ElemType> &other) { this->mSize = other.mSize; this->mLen = other.mLen; this->mpArr = new ElemType[mSize]; for(int i=0; i<mLen; i++) { mpArr[i] = other.mpArr[i]; } } template <typename ElemType> myArrayList<ElemType> & myArrayList<ElemType>::operator =(myArrayList<ElemType> &other) { if(this == &other) return *this; delete []mpArr; this->mSize = other.mSize; this->mLen = other.mLen; this->mpArr = new ElemType[mSize]; for(int i=0; i<mLen; i++) { mpArr[i] = other.mpArr[i]; } return *this; } //修改补充 Student类,要求按成绩排序(从高到低) class Student { private: int mId; float height; int score; public: Student(int id=0,float h=0,int s=0):height(h),mId(id),score(s) { } friend bool operator <(Student &a1,Student &a2) { if(a1.score > a2.score) return true; else return false; } friend ostream &operator <<(ostream &out,Student &s) { out<<"ID:"<<s.mId<<" Height:"<<s.height<<" Score:"<<s.score<<endl; return out; } }; ``` 说明: - `sort()`函数实现排序功能,采用冒泡排序算法; - `myArrayList`类的拷贝构造函数和赋值运算符重载函数均实现深拷贝功能; - `Student`类的 `<` 运算符重载函数改为按成绩从高到低排序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值