美文网首页
九、this指针、全局函数与成员函数、数组类重载练习

九、this指针、全局函数与成员函数、数组类重载练习

作者: 木鱼_cc | 来源:发表于2018-06-05 19:18 被阅读0次

1.this指针

1.png
#include <iostream>
using namespace std;

class Test
{
  public:
    Test(int a, int b,int k)//-­‐-­‐-­‐>Test(Test*this,int a,int b)
   {
     this‐>a = a;
     this­‐>b = b;
     this->m_k = k;
   }

    int getK() {//int getK(Test *const this) this是一个常指针
       //this++;不能修改地址,但可以修改this的内容
       this->m_k = 0;//允许
       return this->m_k;
    }
========替换==============
    int getK() const {//代表int getK(const Test *const this) this是一个常指针
       //this++;不能修改地址
       //this->m_k = 0;//不允许!
       return this->m_k;
    }

   void printT()
   {
    cout<<"a:"<<a<<endl;
    cout<<"b:"<<this­‐>b<<endl;
    cout<<"k:"<<this­‐>m_k<<endl;
   }
 protected:
 private:
    int a;
    int b;
    int m_k;
};

int main(void)
{
   Test t1(1,2,3);//===>Test(&t1,1,2,3);
   t1.printT();//===>printT(&t1)

   return 0;
}
静态成员函数只能访问静态数据成员。原因:非静态成员函数,在调用时this 指针被当作参数传进。而静态成员函数属于类,而不属于对象,没有 this 指针!!!

2.全局函数和成员函数

  • 把全局函数转化成成员函数,通过this指针隐藏左操作数
    Test add(Test &t1, Test &t2)===》Test add(Test &t2)
  • 把成员函数转换成全局函数,多了一个参数
    void printAB()===》void printAB(Test *pthis)
  • 函数返回元素和返回引用
Test& add(Test &t2)//*this//函数返回引用
{
   this­‐>a=this­‐>a+t2.getA();
   this­‐>b=this­‐>b+t2.getB();

   return *this;//返回自己
   //temp引用 = *this; *this是谁?就是t2自己!
}

Test add2(Test &t2)//*this//函数返回元素
{
  //t3是局部变量
   Test t3(this­‐>a+t2.getA(),this­‐>b+t2.getB());

   return t3;
}

3.数组类封装

---------------------------MyArray.h--------------------------
#ifndef __MYARRAY_H__
#define __MYARRAY_H__

#include <iostream>
class MyArray
{
    public:
          MyArray();//无参构造函数
          MyArray(int len);//在给数组初始化的时候,开辟多少个元素空间
          MyArray(const MyArray &another);//拷贝构造函数

          int getLen();      
          void setData(int index,int data);//index代表要赋值的数组的下标,data是数据      
          int getData(int index);    

      //重载中括号[]  取值用途,加&有赋值用途!!!因为10=10是无效的,用变量作为接受才能有赋值效果!
      int& operator [](int index) const;

      friend ostream & operator <<(ostream &os,const MyArray &array)

      friend istream & operator>>(istream &is,MyArray& array);

     //重载"="号单目运算符
      MyArray & operator=(const MyArray& array);

    //重载"=="双目运算符
      int operator==(const MyArray& array);

      ~MyArray();
   private:
         int length;//数组目前的元素个数
         int *space;//soace数在堆开辟数组空间的首地址元素
};
#endif

---------------------------MyArray.cpp--------------------------
#include "MyArray.h"

MyArray::MyArray()
{
     this->space = NULL;
     this->length = 0;
}

MyArray::MyArray(int len)
{
    if(len <= 0){
       this->length =0;
       return ;
    }
    else{
       this->length = len;
       this->space = new int[this->length];//在堆上开辟了长度为length的连续int空间
       cout<<"数组的构造函数MyArray::MyArray(int len)被调用"<<endl;
    }
}

MyArray::MyArray(const MyArray &another)
{
    if(another.length >= 0){
       this->length = another.length;
       this->space = new int[this->length];
       for(int i = 0;i<this->length;i++){
          this->space[i] = another.space[i];
        }
    }
}

int MyArray::getLen(){
      return this->length;
}

void MyArray::setData(int index,int data){
       if(this->space != NULL && this->length > index)
       this->space[index] = data;
       return *this;
}

int MyArray::getData(int index){
     return this->space[index];
}

//重载中括号[]
int& MyArray::operator[](int index) const{
//可以取值array[i]
//operator[](MyArray *this,int index),为了接受const,需要保护this地址
    return this->space[index];
}

//重载<<运算符
//operator<<(cout,array1)
ostream & MyArray::operator <<(ostream &os,const MyArray &array)//加const会报错!为什么?(已更正)
{
   os<<"遍历了整个数组"<<endl;
   for(int i =0;i<array.length;i++){
      os<<array[i]<<endl;
     //array.operator[](i);
     //array.operator[](&array,i)
    //若array是const类型,在重构[]的函数里,一个const类型的地址给到一个非const的地址,C++不允许用一个非const的变量接受const变量,所以报错,需要在[]重载函数里面对this指针加const
   }
   return os;
}

istream & MyArray::operator>>(istream &is,MyArray& array)
{
    cout<<"请输入 "<<array.length<<"个数"<<endl;
    for(int i = 0;i<array.length;i++){
       is>>array[i];
    }
   return is;
}

//重载=号操作符
MyArray & MyArray::operator=(const MyArray& array)
{
    if(this->space !=NULL){
        delete[] this->space;
        this->length = 0;
        this=>space = 0;
    }
  if(array.length > 0 ){
   int len = array.length;
   this->length = len;
   this->space = new int[len];
   for(int i = 0;i < len;i++){
       this->space[i] = array.space[i];
     }
  }
  return *this;
}

 //重载"=="双目运算符
int MyArray::operator==(const MyArray& array){
    if(this->length != array.length)return 0;
    for(int i = 0;i<array.length;i++){
         if(this->space[i] != array.space[i])return 0;
     }
    return 1;
}

MyArray::~MyArray()
{
    if(this->space != NULL){
        //此时space有空间
        delete[] this->space;
        this->length = 0;
        this->space = NULL;
         cout<<"数组的析构函数MyArray::~MyArray()被调用"<<endl;
     }
}

---------------------------main.cpp--------------------------
#include <iostream>
#include "MyArray.h"
using namespace std;
int main(void)
{
  MyArray array1(10);//调用MyArray有参构造函数
    
   //设置一个数组的所有元素
  for(int i = 0;i < array1.getLen();i++){
            array[i] = i+10; 
    //想用array[i] = i + 10;中括号操作
   }

  //遍历一个数组的所有元素
/* 
   for(int i = 0;i < array1.getLen();i++){
          cout<<array1[i]<<endl;//重载[]后
  }
*/
  cout<<array1<<endl;
  cout<<"-----以下是拷贝构造函数------"<<endl;
   

  MyArray array2 = array1;

/*
  for(int i = 0;i < array2.getLen();i++){
          cout<<array2[i]<<endl;
  }
*/
  cout<<array2<<endl;

  MyArray array3(5);
  cin>>array3;
  cout<<array3;

  return 0;
}

相关文章

网友评论

      本文标题:九、this指针、全局函数与成员函数、数组类重载练习

      本文链接:https://www.haomeiwen.com/subject/chkmsftx.html