版本一 返回建立在栈上的对象

  1. #include<iostream>
  2. using namespace std;
  3. class Rational {
  4. public:
  5. Rational(int m,int n) : m(m),n(n) {}
  6. int getM() { return m; }
  7. int getN() { return n; }
  8. private:
  9. int m;
  10. int n;
  11. friend const Rational& operator*(const Rational& lhs,const Rational& rhs);
  12. };
  13. const Rational& operator*(const Rational& lhs,const Rational& rhs) {
  14. Rational r(lhs.m * rhs.m,lhs.n * rhs.n);
  15. return r;
  16. }
  17. int main() {
  18. Rational r1(1,4);
  19. Rational r2(3,5);
  20. Rational r3 = r1 * r2;
  21. return 0;
  22. }

image.png
问题在于:返回了一个reference指向了local对象,local对象在函数结束时会被销毁,reference指向的是一个被销毁的对象,会造成无定义行为,如果返回了一个指针也是一样的道理
C++未定义行为
C++未定义行为

版本二 返回建立在堆上的对象

  1. const Rational& operator*(const Rational& lhs,const Rational& rhs) {
  2. Rational *r = new Rational(lhs.m * rhs.m,lhs.n * rhs.n);
  3. return *r;
  4. }
  5. int main() {
  6. Rational r1(1,4);
  7. Rational r2(3,5);
  8. Rational r3(1,2) ;
  9. Rational r4 = r1 * r2 * r3;
  10. return 0;
  11. }

问题:导致了内存泄漏,operator=返回的指针无法delete

最初尝试函数返回一个引用,目的是 提高效率,减少调用构造函数的次数,有没有方法可以不去调用构造函数呢?

版本三 返回static对象

  1. const Rational& operator*(const Rational& lhs,const Rational& rhs) {
  2. static Rational r;
  3. r = Rational(lhs.m * rhs.m,lhs.n * rhs.n);
  4. return r;
  5. }
  6. bool operator==(const Rational& lhs,const Rational& rhs) {
  7. if(lhs.m == rhs.m && lhs.n == rhs.n) {
  8. return true;
  9. }
  10. return false;
  11. }
  12. int main() {
  13. Rational r1(1,4);
  14. Rational r2(3,5);
  15. Rational r3(1,2);
  16. Rational r4(3,7);
  17. if((r1 * r2) == (r3 * r4)) {
  18. cout << "ture" << endl;
  19. }
  20. else {
  21. cout << "false" << endl;
  22. }
  23. return 0;
  24. }

输出true
如果把operator*的返回值不设为引用类型,就输出false

  1. operator==(operator*(r1,r2),operator*(r3,r4))

问题:
1、两个operaotr都返回一个static的引用,而且返回的还是引用类型,即都是一个引用指向static r这个空间,r1 r2和r3 * r4其实是一块空间;如果返回的不是引用类型,那么调用者得到的是返回值的副本,并不是一块空间
2、static还会造成线程安全问题

版本四 正确的写法

  1. inline const Rational operator*(const Rational& lhs,const Rational& rhs) {
  2. return Rational(lhs.m * rhs.m,lhs.n * rhs.n);
  3. }

首先返回一个栈上的对象,没有内存泄漏问题,其次调用者获得的是该对象的副本,保证结果正确,缺陷就是避免不了返回值的构造和析构。
选择一个正确的写法,虽然产生了效率问题,但是编译器会为我们优化(某些情况下,返回值的构造函数和析构函数也许会被安全的消除),在保证效率的前提下是保证结果正确。

拷贝赋值操作

为什么类的拷贝构造参数加引用、重载赋值函数的返回值和参数加引用

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4. class StringClass {
  5. public:
  6. StringClass(const char* str = "") {
  7. m_str = new char[strlen(str) + 1];
  8. strcpy(m_str,str);
  9. cout << "构造函数" << endl;
  10. }
  11. StringClass(const StringClass& str) {
  12. m_str = new char[strlen(str.m_str) + 1];
  13. strcpy(m_str,str.m_str);
  14. cout << "拷贝构造" << endl;
  15. }
  16. StringClass& operator=(const StringClass& str) {
  17. delete []m_str;
  18. m_str = new char[strlen(str.m_str) + 1];
  19. strcpy(m_str,str.m_str);
  20. cout << "拷贝赋值操作符" << endl;
  21. return *this;
  22. }
  23. ~StringClass() {
  24. delete []m_str;
  25. m_str = nullptr;
  26. }
  27. private:
  28. char *m_str;
  29. };
  30. int main() {
  31. StringClass str1("a");
  32. StringClass str4("");
  33. str4 = str1;
  34. return 0;
  35. }

image.png
1、如果拷贝赋值操作返回值不是引用
image.png
2、如果不自定义拷贝构造,且拷贝赋值操作符返回值不是引用:
image.png
如果拷贝赋值操作返回值不是引用的话,执行到return语句,会调用拷贝构造生成一个临时对象返回给调用端,然后临时对象被销毁。如果类内成员变量有指针的话,那么这个拷贝构造一定是自定义的,因为默认拷贝构造是浅拷贝,会造成内存泄漏。
3、其他
为什么delete指针之后要赋值为空?
C++中,能不能delete空指针
空指针