运算符重载问题,赋值运算符被跳过

img

#include
using namespace std;
class Person {
    
public:  Person();
      int* m_age;
      int m_score;
      Person& operator+(const Person& p) {
          *this->m_age = *this->m_age + *p.m_age;
          this->m_score = this->m_score + p.m_score;
          return *this;
      }
      Person& operator=(const Person &p){
          this->m_score = p.m_score;
          if (this->m_age != NULL) {
              delete this->m_age;
              this->m_age = NULL;
          }
          this->m_age = new int(*p.m_age);
          return *this;
      }
      ~Person()
      {
          if (this->m_age != NULL) {
              delete this->m_age;
              this->m_age = NULL;
      }

      }

};
Person::Person() {
    m_age = new int(10);
    m_score = 10;
}
void test01() {
    Person p1;
    Person p2;
    cout << *p1.m_age << "  " << p1.m_score << endl;
    cout << *p2.m_age << "  " << p2.m_score << endl;
    /*Person p3 = p1 + p2; */
    p1 = p2;
    //cout << *p3.m_age << "  " << p3.m_score << endl;
}

int main() {
    test01();

}

如图,刚学到运算符重载,我在函数里写了加号和等号的重载,但是在有

c++ Person p3 = p1 + p2; 的时候用调试没有调用重载赋值运算符,只调用了加号重载运算符,导致内存泄漏,注释后用p1=p2又调用了等号重载函数,这是为啥?

  • 你可以参考下这个问题的回答, 看看是否对你有帮助, 链接: https://ask.csdn.net/questions/6899169
  • 这篇博客你也可以参考下:C语言:求出二维数组每列中最小元素,并依次放入p,p所指一维数组中。二维数组中的数已在主函数中赋
  • 除此之外, 这篇博客: 常量指针、指针常量,指针数组、数组指针,函数指针、指针函数中的 2.1 指针数组 *p1[5] 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  •  数据类型    *数组名[常量表达式][常量表达式]...... ;
    

    它是一个数组,数组的元素都是指针,数组占多少个字节由数组本身的大小决定,每个元素都是一个指针。

    例如:char *arr[]={“Sunday”,“Monday”},存储了两个指针,第一个指针指向了字符串"Sunday",第二个指针指向了字符串"Monday",而sizeof(arr)=8,因为在32位平台,指针类型大小占4个字节。指针数组最重要的用途是对多个字符串进行处理操作,因为字符指针比二维数组更快更有效。

    在这里插入图片描述
    下面是个简单的例子

    #include <stdio.h>
    int main()
    {
        //定义三个整型数组
    	int a[5] = { 1,2,3,4,5 };
    	int b[5] = { 6,4,8,3,1 };
    	int c[5] = { 2,5,8,6,1 };
        //定义一个存放指向整型变量的指针的数组arr
        int* arr[] = { a,b,c };
        //通过接引用打印出三个一维数组的元素
    	for (int i = 0; i < 3; i++)
    	{
    		for (int j = 0; j < 5; j++)
            {
    		    printf("%d ", *(arr[i]+j));
    	    }
            printf("\n");
        }
    	return 0;
    }
    

    结果如下:

      1 2 3 4 5
    
      6 4 8 3 1
    
      2 5 8 6 1
    

    以上对arr解引用的方式有很多,它们都是等价的,我们来举个例子:

    #include<stdio.h>
    int main()
    {
    	int i = 0;
    	int a[3][4] = { {1,2,3,4} ,{5,6,7,8} ,{9,10,11,12} };//定义一个二维数组
    	int* pa[3];//定义一个指针数组
    	for (i = 0; i < 3; i++)//给指针数组赋值
    		pa[i] = a[i];
        printf("指针数组的内容为:\n");
    	for (i = 0; i < 3; i++)//打印出指针数组的内容
    	{
    		int j;
    		for (j = 0; j < 4; j++)
    			printf("%d ", *(*(pa + i) + j));
    		printf("\n");
    	}
        //以下均为不同方式的解引用操作
        printf("不同解引用操作的结果为:\n");
    	printf("%d,%d\n", a[1][1], *(pa[1] + 1));
    	printf("%d,%d\n", a[1][1], *(*(pa+1) + 1));
    	printf("%d,%d\n", a[1][1], (*(pa + 1))[1]);
    	printf("%d,%d\n", a[1][1], pa[1][1]);
        return 0;
    }
    

    结果如下所示:

    指针数组的内容为:
    
    1 2 3 4
    5 6 7 8
    9 10 11 12
    
    不同解引用操作的结果为:
    
    6,6
    6,6
    6,6
    6,6
    

    从以上例子可看出解引用有多种方式,它们的等价形式如下:

    *( pa[i] + j )         //等价于 *( a[i] + j )
    
    *( *(p+i) + j )        //等价于 *( *(a+j) + j )
    
    ( *(p+i) )[ j ]        //等价于( *(a+i) )[ j ]
    
    p[ i ][ j ]                //等价于 a[i][j]
    

    补充(1):指针数组还可以和字符串数组相结合使用,请看下面的例子:

    #include <stdio.h>
    int main(){
        char *str[3] = {"lirendada","C语言","C Language"};
     
        printf("%s\n%s\n%s\n", str[0], str[1], str[2]);
        return 0;
    }
    

    结果如下:

    lirendada
    
    c语言
    
    C Language
    

    需要注意的是,字符数组 str 中存放的是字符串的首地址,不是字符串本身,字符串本身位于其他的内存区域,和字符数组是分开的。

    也只有当指针数组中每个元素的类型都是char *时,才能像上面那样给指针数组赋值,其他类型不行。

    为了便于理解,可以将上面的字符串数组改成下面的形式,它们都是等价的。

    #include <stdio.h>
    int main(){
        char *str0 = "lirendada";
        char *str1 = "C语言";
        char *str2 = "C Language";
        char *str[3] = {str0, str1, str2};
        printf("%s\n%s\n%s\n", str[0], str[1], str[2]);
        return 0;
    }
    

    补充(2):二维数组与指针数组的区别

    char *p1[]={"lirendada","C","C++"};
    char p2[][8]={"liren","C","C++"};
    
    *p1,*(p1+1)*(p1+2):所指向的字符串常量是不规则长度的,且sizeof(p1)=12。
    
    
    p2[0],p2[1],p2[2]所指向的字符串都是一定长度的,且sizeof(p2)=24