c++学习笔记(4.c++中新的关键字)
来源:互联网 发布:java 读取tar.gz 编辑:程序博客网 时间:2024/06/08 09:28
本节知识点:
1.关键字new与delete:
#include <stdio.h>#include <malloc.h>int main(){/*使用new申请单个变量*/ double* p = new double; *p = 7.2; printf("%f\n",*p);delete p; int *p1 = (int*)malloc(sizeof(int)*10);for(int i = 0; i < 10; i++){*(p1+i) = i;} for(int i = 0; i < 10; i++){printf("%d\n",p1[i]);} free(p1);/*使用new申请一段连续的内存空间*/ //new出来的这个空间看着像数组 但它跟数组有本质区别 这是在堆区的 int *p2 = new int[10]; for(int i = 10; i > 0; i--){p2[10-i] = i;}for(int i = 0; i < 10; i++){printf("%d\n",*(p2+i));}delete [] p2;return 0;}
#include <stdio.h>int main(){int* p = new int(6);printf("%d\n",*p);double* fp = new double(7.2);printf("%f\n",*fp);char* cp = new char('d');printf("%c\n",*cp);return 0;}
2.c++中的命名空间:
namespace name2 //这个是命名空间的嵌套 {int c = 2;int f = 5;namespace name3{int d = 3;int e = 4;}}
#include<stdio.h>namespace name1{int a = 9;namespace name1{int a = 10; }}namespace name2{int p = 19;int a = 0; }int main(){using namespace name1;using name2::a;printf("%d\n",a);return 0;}
3.c++中的强制类型转换:
#include <stdio.h>int main(){int a = 99;double b = static_cast<double>(a);printf("%f\n",b);printf("%c\n",static_cast<char>(b));getchar();return 0;}
#include <stdio.h>int main(){/*const int &j 去除const属性*/const int& j = 4;const_cast<int&>(j) = 6; //其实实际上j就是一个只读变量 利用指针也可以改变j的值的 //*(int*)(&j) = 6; //这步的强制类型转换与上面那句代码等效 printf("j is %d\n",j);int& i = const_cast<int&>(j); i = 12;printf("j is %d\n",i);/*const int* p 去除const属性*/int a = 1;const int* p = &a;*const_cast<int*>(p) = 2; // *(int* )p = 2; //与上面代码等效的强制类型转换 printf("*p is %d\n",*p);/*const int l = 6*/const int l = 16;const_cast<int&>(l) = 17; // (int&)l = 17; //与上面语句等效的强制类型转换 // *const_cast<int*>(&l) = 17; //*(int*)&l = 17;printf("l is %d\n",l);return 0;}
#include <stdio.h>int main(){int a = 10;int &b = a;char &c = reinterpret_cast<char&>(b);printf("%d\n",reinterpret_cast<char&>(b));a = 129;printf("a is %d\n",a);printf("b is %d\n",b);printf("c is %d\n",c);return 0;}
#include <stdio.h>int main(){int a = 10;*reinterpret_cast<unsigned int*>(&a) = 12; printf("a is %d\n",a);int* b = &a;char* c = reinterpret_cast<char*>(b);*c = 14;printf("a is %d\n",*c);return 0;}
0 0
- c++学习笔记(4.c++中新的关键字)
- c++学习笔记(4.c++中新的关键字)
- C#.NET验证码智能识别学习笔记---02-C#.Net中Using关键字的用法
- C语言中volatile关键字的学习
- C语言学习笔记 (一) C语言的关键字
- 学习C中volatile关键字
- 我的小小学习笔记(3)----C的关键字
- C#:params 关键字的学习
- C#:params 关键字的学习
- <<C语言深度剖析>>学习笔记之一:C语言中32个关键字
- C 关键字的秘密 中
- static关键字用法学习笔记(C++)
- static关键字用法学习笔记(C++)
- C语言学习笔记 逻辑结构关键字
- c语言学习笔记之数据类型、关键字
- C语言学习中遇到的一些关键字
- Objective-C中copy 、retain以及ARC中新加入的strong、weak关键字的含义
- Objective-C中copy 、retain以及ARC中新加入的strong、weak关键字的含义
- 产品研发核心价值流中的四大业务主线(计划控制线、质量保证线、预算费用线、工程技术线)
- Eclipse 修改注释的 date time 日期时间格式,即${date}变量格式
- 数据结构与算法——直接插入排序
- sublime text 3中文乱码问题解决办法
- cocos2dx 3.x 内存管理总结
- c++学习笔记(4.c++中新的关键字)
- SSL/TLS协议运行机制的概述
- 性能测试面试题汇总(三)
- Redhat下在R中安装RODBC和RMySQL包
- 性能测试面试题汇总(四)
- Django 处理模糊搜索结果
- Linux下MySQL-python的安装
- 小白用advanced installer建安装包
- n*m 矩阵: set matrix zeroes