1. new,delete的局部重载:

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int objs = ;
  5.  
  6. class myclass
  7. {
  8. public:
  9. myclass()
  10. {
  11. //objs++;
  12. cout << "create" << endl;
  13. }
  14. ~myclass()
  15. {
  16. //objs--;
  17. cout << "delete" << endl;
  18. }
  19. //operator重载,针对new重新作出一种解释,只针对当前类
  20. static void * operator new(size_t size)
  21. {
  22. objs++;
  23. cout << "new-call" << endl;
  24. myclass *p = ::new myclass; //全局new
  25. return p;
  26. }
  27. static void operator delete(void *p)
  28. {
  29. objs--;
  30. cout << "delete-call" << endl;
  31. ::delete p;
  32. }
  33. };
  34. //功能1:无法在堆上被创建的类
  35. void main()
  36. {
  37. myclass *p1 = new myclass;
  38. myclass *p2 = new myclass;
  39. myclass *p3 = new myclass;
  40. delete p1;
  41.  
  42. int *p = new int(); //此时new重载对于这一句无效
  43.  
  44. cout << objs << endl;
  45.  
  46. cin.get();
  47. }

    

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int objs = ;
  5. void *g_p = nullptr;
  6.  
  7. class myclass
  8. {
  9. public:
  10. myclass()
  11. {
  12. //objs++;
  13. cout << "create" << endl;
  14. }
  15. ~myclass()
  16. {
  17. //objs--;
  18. cout << "delete" << endl;
  19. }
  20.  
  21. //operator重载,针对new重新作出一种解释,只针对当前类
  22. static void * operator new(size_t size)
  23. {
  24. if (g_p==nullptr)
  25. {
  26. objs++;
  27. cout << "new-call" << endl;
  28. myclass *p = ::new myclass; //全局new
  29. g_p = p; //单例,堆上创建对象只有一个
  30. return p;
  31. }
  32. else
  33. {
  34. return g_p;
  35. }
  36. }
  37.  
  38. static void operator delete(void *p)
  39. {
  40. if (g_p!=nullptr)
  41. {
  42. objs--;
  43. cout << "delete-call" << endl;
  44. ::delete p;
  45. g_p = nullptr;
  46. }
  47. }
  48. };
  49. //功能1:无法在堆上被创建的类
  50. //功能2:实现统计分配内存,释放内存的次数
  51. //实现单例设计模式,实现避免反复delete出错
  52. //new delete在内部,只针对当前类,int double 无影响
  53. void main()
  54. {
  55. myclass *p1 = new myclass;
  56. myclass *p2 = new myclass;
  57.  
  58. delete p1;
  59. delete p1; //规避了两次delete的错误
  60.  
  61. cin.get();
  62. }

    

2. 全局new,delete重载:

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. //全局内存管理,统计释放内存,分配内存
  5. //new new [] delete delete []
  6. //分配内存优先于构造
  7. //析构优先于释放内存
  8. void * operator new(size_t size)
  9. {
  10. cout << "g_new call" << endl;
  11. void *p = malloc(size); //全局的new,只能使用malloc
  12. return p;
  13. }
  14. void * operator new [](size_t size)
  15. {
  16. cout << "g_new [] call" << endl;
  17. cout << size << endl;
  18. return operator new(size); //每个元素调用一次new
  19. }
  20.  
  21. void operator delete(void *p)
  22. {
  23. cout << "g_delete call" << endl;
  24. free(p);
  25. }
  26. void operator delete [](void *p)
  27. {
  28. cout << "g_delete [] call" << endl;
  29. free(p);
  30. }
  31.  
  32. class myclass
  33. {
  34. public:
  35. myclass()
  36. {
  37. cout << "create call" << endl;
  38. }
  39. ~myclass()
  40. {
  41. cout << "delete call" << endl;
  42. }
  43. };
  44.  
  45. void main()
  46. {
  47. int *p1 = new int();
  48. delete p1;
  49.  
  50. myclass *p2 = new myclass;
  51. delete p2;
  52.  
  53. myclass *px = new myclass[];
  54. delete[]px;
  55.  
  56. cin.get();
  57. }

    

3. 绑定类成员函数:

  1. #include <iostream>
  2. #include <functional>
  3.  
  4. using namespace std;
  5. using namespace std::placeholders;//站位
  6.  
  7. struct MyStruct
  8. {
  9. void add1(int a)
  10. {
  11. cout << a << endl;
  12. }
  13. void add2(int a, int b)
  14. {
  15. cout << a << b << endl;
  16. }
  17. void add3(int a, int b, int c)
  18. {
  19. cout << a << b << c << endl;
  20. }
  21. };
  22.  
  23. void main()
  24. {
  25. MyStruct my1;
  26. //my1.add(10);
  27.  
  28. //绑定包装器,包装类成员函数,用于使用
  29. auto fun1 = bind(&MyStruct::add1, &my1, _1);//有1个参数 函数名、对象地址、参数
  30. fun1();
  31.  
  32. auto fun2 = bind(&MyStruct::add2, &my1, _1,_2);//有2个参数
  33. fun2(,);
  34.  
  35. auto fun3 = bind(&MyStruct::add3, &my1, _1,_2,_3);//有3个参数
  36. fun3(,,);
  37.  
  38. cin.get();
  39. }

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

  1. #include <iostream>
  2. #include <functional>
  3.  
  4. using namespace std;
  5. using namespace std::placeholders;
  6.  
  7. int add(int a, int b,int c)
  8. {
  9. return a + b+c;
  10. }
  11.  
  12. struct MyStruct
  13. {
  14. int operator()(int a,int b) //仿函数
  15. {
  16. return a + b;
  17. }
  18. };
  19.  
  20. void main()
  21. {
  22. auto fun1 = bind(add, ,, _1);//适配器模式
  23. cout << fun1() << endl; //
  24.  
  25. auto fun2 = bind([](int a, int b)->int {return a + b; }, , _1);
  26. cout << fun2() << endl; //
  27.  
  28. MyStruct my1;
  29. cout << my1(, ) << endl; //
  30. auto fun3 = bind(my1, , _1); //绑定
  31. cout << fun3() << endl; //
  32.  
  33. cin.get();
  34. }

5. 静态断言:

  1. #include <iostream>
  2. #include <cassert>
  3. using namespace std;
  4.  
  5. int divv(int a, int b)
  6. {
  7. assert(b != ); //断言
  8. return a / b;
  9. }
  10.  
  11. void main()
  12. {
  13. cout << divv(, ) << endl;
  14.  
  15. cin.get();
  16. }
  1. #include <iostream>
  2. #include <cassert>
  3. using namespace std;
  4.  
  5. static_assert(sizeof(void *) >= , "environment is not 64!");
  6.  
  7. void main()
  8. {
  9.  
  10. cin.get();
  11. }

    

6. 内联函数:

  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4.  
  5. #define f(x) x*x*x //C语言内联,C++要求类型严格匹配
  6.  
  7. inline int get(int x) //C++的内联函数
  8. {
  9. return x*x*x;
  10. }
  11. //提高程序运行速度
  12.  
  13. //inline 只是对于编译器的建议
  14. //一般情况下,我们对内联函数做如下的限制:
  15. //(1)不能有递归;
  16. //(2)不能包含静态数据;
  17. //(3)不能包含循环;
  18. //(4)不能包含switch和goto语句;
  19. //(5)不能包含数组。
  20. //若一个内联函数定义不满足以上限制,则编译系统把它当做普通函数对待
  21.  
  22. template<class T>
  23. inline T go(T t)
  24. {
  25. return t*t;
  26. }
  27.  
  28. void main()
  29. {
  30. get();
  31.  
  32. go(); //优化为内联函数
  33.  
  34. auto fun = []() {}; //lambda表达式实际上也是内联函数
  35.  
  36. cin.get();
  37. }

7. CPP处理转义字符:

  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. void main()
  8. {
  9. //string str("\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe\"");
  10. string str(R"("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe")"); // R"(......)"
  11.  
  12. system(str.c_str());
  13.  
  14. cin.get();
  15. }

C++与C的区别二的更多相关文章

  1. C#中抽象类和接口的区别(二)

    一.抽象类: 抽象类是特殊的类,只是不能被实例化:除此以外,具有类的其他特性:重要的是抽象类可以包括抽象方法,这是普通类所不能的.抽象方法只能声明于抽象类中,且不包含任何实现,派生类必须覆盖它们.另外 ...

  2. Python协程与Go协程的区别二

    写在前面 世界是复杂的,每一种思想都是为了解决某些现实问题而简化成的模型,想解决就得先面对,面对就需要选择角度,角度决定了模型的质量, 喜欢此UP主汤质看本质的哲学科普,其中简洁又不失细节的介绍了人类 ...

  3. vue1.0和vue2.0的区别(二)

    这篇我们继续之前的vue1.0和vue2.0的区别(一)继续说 四.循环 学过vue的同学应该知道vue1.0是不能添加重复数据的,否则它会报错,想让它重复添加也不是不可以,不过需要定义别的东西 而v ...

  4. Linux内存管理--虚拟地址、逻辑地址、线性地址和物理地址的区别(二)【转】

    本文转载自:http://blog.csdn.net/yusiguyuan/article/details/9668363 这篇文章中介绍了四个名词的概念,下面针对四个地址的转换进行分析 CPU将一个 ...

  5. java集合对象区别二

    集合包是Java中最常用的包,它最常用的有Collection和Map两个接口的实现类,Collection用于存放多个单对象,Map用于存放Key-Value形式的键值对. Collection中常 ...

  6. OC与Swift的区别二(常量、变量、运算符)

    4.常量与变量声明 oc的变量声明使用  类型 变量名 = 变量值的方式,其中类型为系统内置数据类型或自定义类型,变量名需由英文字母开头且不能包含特殊字符 swift变量声明使用 var 变量名 = ...

  7. 传统IO与NIO区别二

    nio是new io的简称,从jdk1.4就被引入了.现在的jdk已经到了1.6了,可以说不是什么新东西了.但其中的一些思想值得我来研究.这两天,我研究了下其中的套接字部分,有一些心得,在此分享.  ...

  8. Cocos2d中update与fixedUpdate的区别(二)

    关于update:方法的目的 update:方法的目的在于给你一个更新你的游戏(你游戏中的所有对象,标签等待)的机会,在它们被渲染到屏幕之前. 换句话说,如果你想要一些游戏对象显示在屏幕的特定位置,你 ...

  9. java static成员变量方法和非static成员变量方法的区别 ( 二 )

    原创文章,未经作者允许,禁止转载!!! 静态成员变量不用new对象,在类加载的过程中就已经初始化存放在数据区域,静态成员变量是类和所有对象共有的,类和对象都可以改变它的值,每一次改变值之后,静态成员变 ...

随机推荐

  1. netbeans下调试php程序-xdebug

    环境说明: pc系统:ubuntu 16.04 php版本:5.6.23 apache:Apache/2.4.18 (Ubuntu) 第一步:修改xdebug.ini 打开文件/etc/php/5.6 ...

  2. smarty if

    <{if data}> <input type="submit" value="修改" /> <{else}> <in ...

  3. linux 查找php.ini在那个文件夹

    第一种方法:通过phpinfo查看 第二种方法: 执行 php -i | grep php.ini 结果如下:

  4. RocketMQ 使用及常见问题

    前言 本文档是针对RocketMQ使用及常见问题的说明. 一.获取项目.安装包及文档 1. alibaba/RocketMQ https://github.com/alibaba/RocketMQ 2 ...

  5. HBase & thrift & C++编程

    目录 目录 1 1. 前言 1 2. 启动和停止thrift2 1 2.1. 启动thrift2 1 2.2. 停止thrift2 1 2.3. 启动参数 2 3. hbase.thrift 2 3. ...

  6. linux每天一小步---alias命令详解

    1 命令功能     alias命令用来设置指令的别名,alias命令设置的别名只限于该次登陆操作,若要每次登入即自动设好别名,可在/etc/profile或自己的~/.bashrc中设定指令的别名. ...

  7. Image 和byte[]之间的转换

    1.Image 转 byte[] public byte[] GetByteByImage(Image image) { byte[] bt = null; try { if (!image.Equa ...

  8. oracle 索引的分类

    1. B树索引(默认索引,保存讲过排序过的索引列和对应的rowid值) 1)说明: 1.oracle中最常用的索引:B树索引就是一颗二叉树:叶子节点(双向链表)包含索引列和指向表中每个匹配行的ROWI ...

  9. 動態修改 XML 欄位

    /* -- for test DECLARE @content VARCHAR(50) DECLARE @folioId VARCHAR(50) DECLARE @opinionType VARCHA ...

  10. php萌新|学习|排坑|のmysqli_error()方法的妙用

    从开始学习php当现在已经有一个月多.除了每天完成公司布置的日常汇报,也没有耐下性子写一写自己想写的东西.今天就当起个头,坚持一周有个两三片文章或者小总结,也不枉费自己的付出.(我自己都不信,你会信吗 ...