C++ 中的返回值优化

代码:

//
// Created by w on 2024/6/19.
//
#include <iostream>

using namespace std;

template<typename T>
class myClass {
public:
    myClass() {
        data = NULL;
        cout << "default construct" << endl;
    }

    myClass(const T &para) {
        data = new T(para);
        cout << "construct with para" << endl;
    }

    myClass(const myClass &other) {
        if (&other == this) {
            return;
        }

        data = new T(*other.data);
        cout << "copy construct" << endl;
    }


    myClass &operator=(const myClass &other) {
        if (&other == this) {
            return *this;
        }
        if (data != NULL) {
            delete data;
        }
        data = new T(*other.data);
        cout << "copy operator =" << endl;

        return *this;
    }

    myClass(myClass &&other) {
        if (&other == this) {
            return;
        }

        data = other.data;
        other.data = NULL;
        cout << "move construct" << endl;
    }


    myClass &operator=(myClass &&other) {
        if (&other == this) {

            return *this;
        }

        if (data != NULL) {
            delete data;
        }

        data = other.data;
        other.data = NULL;
        cout << "move operator =" << endl;

        return *this;
    }

    ~myClass() {
        if (data != NULL) {
            delete data;
        }
        cout << "destruct" << endl;
    }

    void print() {
        cout << *data << endl;
    }

private:
    T *data;
};

template<typename T>
myClass<T> f1() {
    return myClass<int>(1000);
}

template<typename T>
myClass<T> f2() {
    myClass<T> namedObj = myClass<int>(1000);
    return namedObj;
}

int main() {
    {
        myClass<int> obj = f1<int>();
    }
    cout << "############" << endl;
    {
        myClass<int> obj = f2<int>();
    }
}

直接运行输出(gcc默认开启了返回值优化):

construct with para
destruct
############
construct with para
move construct
destruct
destruct

关闭返回值优化输出: 

g++ -fno-elide-constructors  classDemo.cpp -std=c++11   && ./a.out 

construct with para  (构造匿名对象)
move construct  (f1 用前面的匿名对象构造一个临时对象返回)
destruct (匿名对象析构)
move construct  (obj使用临时对象move构造)
destruct (临时对象析构)
destruct (obj析构)


############

construct with para  (构造匿名对象)
move construct (使用匿名对象构造具名对象)
destruct (匿名对象析构)
move construct  (使用具名对象构造临时对象)
destruct (具名对象析构)
move construct (使用临时对象构造obj)
destruct (临时对象析构)
destruct (obj析构)
 

相关推荐

  1. C++ 返回优化

    2024-07-14 12:30:03       20 阅读
  2. C++vector返回最高效返回

    2024-07-14 12:30:03       55 阅读
  3. Modern C++ idiom6 - 命名返回优化NRVO

    2024-07-14 12:30:03       46 阅读
  4. C++】表达式返回数据类型

    2024-07-14 12:30:03       49 阅读
  5. C#返回多个方法

    2024-07-14 12:30:03       25 阅读
  6. C#调用C++函数并返回const char*类型

    2024-07-14 12:30:03       50 阅读
  7. C++返回返回引用、返回地址

    2024-07-14 12:30:03       53 阅读
  8. C# 优雅动态序列化接口返回数据

    2024-07-14 12:30:03       38 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-14 12:30:03       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-14 12:30:03       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-14 12:30:03       57 阅读
  4. Python语言-面向对象

    2024-07-14 12:30:03       68 阅读

热门阅读

  1. 2024/7/14 英语每日一段

    2024-07-14 12:30:03       19 阅读
  2. Python爬虫教程第一篇

    2024-07-14 12:30:03       24 阅读
  3. 使用druid对sql进行血缘解析

    2024-07-14 12:30:03       24 阅读
  4. Spring Boot项目的控制器貌似只能get不能post问题

    2024-07-14 12:30:03       32 阅读
  5. Django是干什么的?好用么?

    2024-07-14 12:30:03       25 阅读
  6. HTTPS 加密流程全解析

    2024-07-14 12:30:03       26 阅读
  7. mysql快速精通(四)多表查询

    2024-07-14 12:30:03       23 阅读
  8. 如何隐藏 Ubuntu 顶部状态栏

    2024-07-14 12:30:03       31 阅读
  9. springboot2——功能和原理

    2024-07-14 12:30:03       22 阅读
  10. Oracle 数据清理

    2024-07-14 12:30:03       22 阅读
  11. win32:第一个窗口程序-初始化实例(part.5)

    2024-07-14 12:30:03       26 阅读