「已注销」 2020-03-04 15:21 采纳率: 0%
浏览 309
已采纳

使用const ,引用,函数重载,函数模板,函数默认值,new和delete其中的三个(至少三个)写一个综合型的C++例子!!!

使用const ,引用,函数重载,函数模板,函数默认值,new和delete其中的三个写一个综合型的C++例子!!谢谢各位大佬

  • 写回答

1条回答 默认 最新

  • duoshikongbai 2020-03-08 17:55
    关注

    1、模板函数、const 、&

    template
    T add(const T & a, const T &b)
    {
    return a+b;
    }

    2、自己找一下度娘,一堆实例;还有就是自己多看看概念,正所谓自己动手丰衣足食;

    class ClassA
    {
    public:
    ClassA(){ }

    ClassA(const char* pszInputStr)
    {
        pszTestStr = new char[strlen(pszInputStr) + 1];
        strncpy(pszTestStr, pszInputStr, strlen(pszInputStr) + 1);
    }
    
    ~ClassA(){ delete pszTestStr; }
    
    // 赋值运算符重载函数
    ClassA& operator=(const ClassA& cls)
    {
        // 避免自赋值
        if (this != &cls)
        {
            // 避免内存泄露
            if (pszTestStr != NULL)
            {
                delete pszTestStr;
                pszTestStr = NULL;
            }
    
            pszTestStr = new char[strlen(cls.pszTestStr) + 1];
            strncpy(pszTestStr, cls.pszTestStr, strlen(cls.pszTestStr) + 1);
        }
    
        return *this;
    }
    

    public:
    char* pszTestStr;
    };
    转载自https://blog.csdn.net/liitdar/article/details/80656156

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog