C++与C的区别二

news/2024/7/20 13:33:32 标签: 内存管理, 设计模式, c/c++

1. new,delete的局部重载:

#include <iostream>
using namespace std;

int objs = 0;

class myclass
{
public:
    myclass()
    {
        //objs++;
        cout << "create" << endl;
    }
    ~myclass()
    {
        //objs--;
        cout << "delete" << endl;
    }
    //operator重载,针对new重新作出一种解释,只针对当前类
    static void * operator new(size_t size)
    {
        objs++;
        cout << "new-call" << endl;
        myclass *p = ::new myclass;    //全局new
        return p;
    }
    static void operator delete(void *p)
    {
        objs--;
        cout << "delete-call" << endl;
        ::delete p;
    }
};
//功能1:无法在堆上被创建的类
void main()
{
    myclass *p1 = new myclass;
    myclass *p2 = new myclass;
    myclass *p3 = new myclass;
    delete p1;

    int *p = new int(5);    //此时new重载对于这一句无效

    cout << objs << endl;

    cin.get();
}

    

#include <iostream>
using namespace std;

int objs = 0;
void *g_p = nullptr;

class myclass
{
public:
    myclass()
    {
        //objs++;
        cout << "create" << endl;
    }
    ~myclass()
    {
        //objs--;
        cout << "delete" << endl;
    }

    //operator重载,针对new重新作出一种解释,只针对当前类
    static void * operator new(size_t size)
    {
        if (g_p==nullptr)
        {
            objs++;
            cout << "new-call" << endl;
            myclass *p = ::new myclass;    //全局new
            g_p = p;    //单例,堆上创建对象只有一个
            return p;
        } 
        else
        {
            return g_p;
        }
    }

    static void operator delete(void *p)
    {
        if (g_p!=nullptr)
        {
            objs--;
            cout << "delete-call" << endl;
            ::delete p;
            g_p = nullptr;
        } 
    }
};
//功能1:无法在堆上被创建的类
//功能2:实现统计分配内存,释放内存的次数
//实现单例设计模式,实现避免反复delete出错
//new delete在内部,只针对当前类,int double 无影响
void main()
{
    myclass *p1 = new myclass;
    myclass *p2 = new myclass;

    delete p1;
    delete p1;    //规避了两次delete的错误

    cin.get();
}

    

2. 全局new,delete重载:

#include <iostream>
using namespace std;

//全局内存管理,统计释放内存,分配内存
//new        new []        delete        delete []
//分配内存优先于构造
//析构优先于释放内存
void * operator new(size_t size)
{
    cout << "g_new call" << endl;
    void *p = malloc(size);    //全局的new,只能使用malloc
    return p;
}
void * operator new [](size_t size)
{
    cout << "g_new [] call" << endl;
    cout << size << endl;
    return operator new(size);    //每个元素调用一次new
}

void operator delete(void *p)
{
    cout << "g_delete call" << endl;
    free(p);
}
void operator delete [](void *p)
{
    cout << "g_delete [] call" << endl;
    free(p);
}

class myclass
{
public:
    myclass()
    {
        cout << "create call" << endl;
    }
    ~myclass()
    {
        cout << "delete call" << endl;
    } 
};

void main()
{
    int *p1 = new int(5);
    delete p1;

    myclass *p2 = new myclass;
    delete p2;

    myclass *px = new myclass[10];
    delete[]px;

    cin.get();
}

    

 3. 绑定类成员函数:

#include <iostream>
#include <functional>

using namespace std;
using namespace std::placeholders;//站位

struct MyStruct
{
    void add1(int a)
    {
        cout << a << endl;
    }
    void add2(int a, int b)
    {
        cout << a << b << endl;
    }
    void add3(int a, int b, int c)
    {
        cout << a << b << c << endl;
    }
};

void main()
{
    MyStruct my1;
    //my1.add(10);

    //绑定包装器,包装类成员函数,用于使用
    auto fun1 = bind(&MyStruct::add1, &my1, _1);//有1个参数  函数名、对象地址、参数
    fun1(10);

    auto fun2 = bind(&MyStruct::add2, &my1, _1,_2);//有2个参数
    fun2(100,200);

    auto fun3 = bind(&MyStruct::add3, &my1, _1,_2,_3);//有3个参数
    fun3(1000,2000,3000);

    cin.get();
}

4. 绑定lambda表达式以及仿函数:

#include <iostream>
#include <functional>

using namespace std;
using namespace std::placeholders;

int add(int a, int b,int c)
{
    return a + b+c;
}

struct MyStruct
{
    int operator()(int a,int b)    //仿函数
    {
        return a + b;
    }
};

void main()
{
    auto fun1 = bind(add, 10,12, _1);//适配器模式
    cout << fun1(1000) << endl;        //1022

    auto fun2 = bind([](int a, int b)->int {return a + b; }, 100, _1);
    cout << fun2(123) << endl;        //223

    MyStruct my1;
    cout << my1(1, 2) << endl;        //3
    auto fun3 = bind(my1, 100, _1);    //绑定
    cout << fun3(100) << endl;        //200
        
    cin.get();
}

5. 静态断言:

#include <iostream>
#include <cassert>
using namespace std;

int divv(int a, int b)
{
    assert(b != 0);    //断言
    return a / b;
}

void main()
{
    cout << divv(1, 0) << endl;

    cin.get();
}
#include <iostream>
#include <cassert>
using namespace std;

static_assert(sizeof(void *) >= 8, "environment is not 64!");

void main()
{

    cin.get();
}

    

6. 内联函数:

#include <iostream>
#include <cstdlib>
using namespace std;

#define f(x) x*x*x        //C语言内联,C++要求类型严格匹配

inline int get(int x)    //C++的内联函数
{
    return x*x*x;
}
//提高程序运行速度

//inline 只是对于编译器的建议
//一般情况下,我们对内联函数做如下的限制:
//(1)不能有递归;
//(2)不能包含静态数据;
//(3)不能包含循环;
//(4)不能包含switch和goto语句;
//(5)不能包含数组。
//若一个内联函数定义不满足以上限制,则编译系统把它当做普通函数对待

template<class T>
inline T go(T t)
{
    return t*t;
}

void main()
{
    get(10);

    go(5);    //优化为内联函数

    auto fun = []() {};    //lambda表达式实际上也是内联函数

    cin.get();
}

7. CPP处理转义字符:

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

void main()
{
    //string str("\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe\"");
    string str(R"("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe")");            //  R"(......)" 

    system(str.c_str());

    cin.get();
}

 

转载于:https://www.cnblogs.com/si-lei/p/9511246.html


http://www.niftyadmin.cn/n/1804921.html

相关文章

ThinkPHP框架实现rewrite路由配置

rewrite路由形式&#xff1a; //网址/分组/控制器/方法 配置实现rewrite路由的配置&#xff1a; 1、 修改apache的配置 先修改httpd.conf配置文件中的AllowOverrideAll&#xff0c;全部修改成All&#xff1a; 2、开启重写模块&#xff1a; 3、修改虚拟目录中的配置&#xff0…

农村环境保护

平时作业一 一、单选题 题目1 要改变农业生产生态环境的现状,( )是重要的方法。 选择一项: A. 减缓土壤退化 B. 减少污水灌溉 C. 减少化肥施用量 D. 减少乡镇企业污染 解析:充分利用农村资源,以秸秆和畜禽粪便等有机废弃物为原料,可以生产生物有机肥和有机复合肥。它可作…

01.JavaScript简介——JavaScript高级程序设计(笔记)

00.要想全面理解和掌握JavaScript&#xff0c;关键在于弄清楚他的本质、历史和局限性01.JavaScript实现*核心(ECMAScript)*文档对象模型(DOM)*浏览器对象模型&#xff08;BOM&#xff09;02.ECMA-262大致内容&#xff1a;语法、类型、语句、关键字、保留字、操作符、对象 03.文…

将excel导入mysql(使用navicat)

来源&#xff1a;https://www.cnblogs.com/xjnotxj/p/5304052.html excel: 注&#xff1a; 1、mysql里建立一张跟excel一样的表结构的表&#xff08;包含id&#xff09; 2、excel最好没有任何格式&#xff0c;只是纯值&#xff0c;不然会出现导入不了的错误 ------------------…

农村环境保护学习

平时作业三 一、单选题 题目1 我国在80年代中期提出了资源化、无害化和减量化作为控制( )污染的技术政策。 选择一项: A. 液体废弃物 B. 农业废弃物 C. 固体废弃物 D. 气体废弃物 我国是在80年代初期开始有限地进行资源化,并于80年代中期提出了“资源化、无害化和减量化”作为…

UWP UI自动化测试(一)------WinAppDriver/Inspect.exe demo

1、首先在启动 WinAppDriver.exe&#xff0c;运行界面&#xff1a; 路径&#xff1a;C:\Program Files (x86)\Windows Application Driver\WinAppDriver.exe 如果不运行它的话&#xff0c;UI自动化工程在运行单元测试的时候&#xff0c;vs 会抛异常&#xff1a; 12345678/* 先启…

VMware15与kali2020安装过程

VMware15安装步骤 1.解压【VMware15】软件安装包&#xff0c;并打开 2.鼠标右击【VMware_setup】&#xff0c;以管理员身份运行 3.等待加载中… 4.点击【下一步】 5.勾选【我接受…】&#xff0c;点击【下一步】 6.点击【更改…】更改软件安装路径&#xff0c;然后点…

linux中sed命令

sed sed意为流编辑器&#xff08;Stream Editor&#xff09;&#xff0c;在Shell脚本和Makefile中作为过滤器使用非常普遍&#xff0c;也就是把前一个程序的输出引入sed的输入&#xff0c;经过一系列编辑命令转换为另一种格式输出。sed和vi都源于早期UNIX的ed工具&#xff0c;所…