-------------------------------------资源来源于网络,仅供自学使用,如有侵权,联系我必删.
第一:
动态内存分配
C++ 中的动态内存分配
―C++ 中通过new 关键字进行动态内存申请
―C++ 中的动态内存申请是基于类型进行的
―delete 关键字用于内存释放
C++ 中的动态内存分配
#include <stdio.h>
int main()
{
int* p = new int;//申请空间,用来存放int型变量
*p = 5; //4个字节里面写上5
*p = *p + 10; //5+10
printf("p = %p\n", p);
printf("*p = %d\n", *p);
delete p; //释放一个单位空间
p = new int[10];//申请空间,用来存放int型数组
for(int i=0; i<10; i++)
{
p[i] = i + 1;
printf("p[%d] = %d\n", i, p[i]);
}
delete[] p; //释放一个数组空间
printf("Press any key to continue...");
getchar();
return 0;
}
new 关键字与malloc 函数的区别
new 关键字是C++ 的一部分,malloc 是由C 库提供的函数
new 以具体类型为单位进行内存分配,malloc 只能以字节为单位进行内存分配
new 在申请单个类型变量时可进行初始化,malloc 不具备内存初始化的特性
第二:
new 关键字的初始化
#include <stdio.h>
int main()
{
//初始化申请到的空间的变量
int* pi = new int(1);
float* pf = new float(2.0f);
char* pc = new char('c');
printf("*pi = %d\n", *pi);
printf("*pf = %f\n", *pf);
printf("*pc = %c\n", *pc);
delete pi;
delete pf;
delete pc;
printf("Press any key to continue...");
getchar();
return 0;
}
第三:
C++中的命名空间
在 C 语言中只有一个全局作用域
C 语言中所有的全局标识符共享同一个作用域
标识符之间可能发生冲突
C++ 中提出了命名空间的概念
命名空间将全局作用域分成不同的部分
不同命名空间中的标识符可以同名而不会发生冲突
命名空间可以相互嵌套
全局作用域也叫默认命名空间
C++ 命名空间的定义
namespace name { /* … */ }
#include <stdio.h>
namespace First
{
int i = 0;
}
namespace Second
{
int i = 1;//同命名空间中的标识符可以同名而不会发生冲突
namespace Internal
{
struct P
{
int x;
int y;
};
}
}
int main()
{
printf("Press any key to continue...");
getchar();
return 0;
}
第四:
C++ 命名空间的使用:
使用整个命名空间:using namespace name;
使用命名空间中的变量:using name::variable;
使用默认命名空间中的变量:::variable
默认情况下可以直接使用默认命名空间中的所有标识符
C++命名空间的使用
#include <stdio.h>
namespace First
{
int i = 0;
}
namespace Second
{
int i = 1;
namespace Internal
{
struct P
{
int x;
int y;
};
}
}
int main()
{
using namespace First;//使用First命名空间中的所有标识符
using Second::Internal::P;//使用Internal命名空间中的标识符P
printf("i = %d\n", i);
printf("i = %d\n", Second::i);
P p = {2, 3};
printf("p.x = %d\n", p.x);
printf("p.y = %d\n", p.y);
printf("Press any key to continue...");
getchar();
return 0;
}
第五:
C 方式的强制类型转换
( Type )( Expression ) or Type( Expression )
#include <stdio.h>
typedef void(PF)(int);//定义PF的函数类型
struct Point //定义结构体
{
int x;
int y;
};
int main()
{
int v = 0x12345;
PF* pf = (PF*)v;//强制类型转换成函数指针
char c = char(v);//将v类型强制转换成chae类型,然后赋值给c
pf(v);//将pf指针作为函数,调用v
Point* p = (Point*)v;//将int类型的v强制转换成Point结构体的地址
printf("p->x = %d\n", p->x);
printf("p->y = %d\n", p->y);
printf("Press any key to continue...");
getchar();
return 0;
}
C 方式强制类型转换存在的问题
第六:
C++ 将强制类型转换分为4种不同的类型
static_cast 强制类型转换
― 用于基本类型间的转换,但不能用于基本类型指针间的转换
― 用于有继承关系类对象之间的转换和类指针之间的转换
#include <stdio.h>
int main()
{
int i = 0x12345;
char c = 'c';
int* pi = &i;
char* pc = &c;
c = static_cast<char>(i);//基本类型转换 ok!
pc = static_cast<char*>(pi);//指针间转换 Oops!
printf("Press any key to continue...");
getchar();
return 0;
}
static_cast 是编译期进行转换的,无法在运行时检测类型,所以类类型之间的转换可能存在风险。
const_cast 强制类型转换
― 用于去除变量的const 属性
#include <stdio.h>
int main()
{
const int& j = 1;
int& k = const_cast<int&>(j);//将常量j变成变量j 引用k k就是j
const int x = 2;
int& y = const_cast<int&>(x);//y是常量地址的别名,普通变量
k = 5;
printf("k = %d\n", k);
printf("j = %d\n", j);
y = 8;
printf("x = %d\n", x);
printf("y = %d\n", y);
printf("&x = %p\n", &x);
printf("&y = %p\n", &y);
printf("Press any key to continue...");
getchar();
return 0;
}
reinterpret_cast强制类型转换
― 用于指针类型间的强制转换
― 用于整数和指针类型间的强制转换
#include <stdio.h>
int main()
{
int i = 0;
char c = 'c';
int* pi = &i;
char* pc = &c;
//指针类型间的强制转换
pc = reinterpret_cast<char*>(pi);
pi = reinterpret_cast<int*>(pc);
c = reinterpret_cast<char>(i); // static_cast用在这里的 Oops!
printf("Press any key to continue...");
getchar();
return 0;
}
reinterpret_cast 直接从二进制位进行复制,是一种极其不安全的转换。
dynamic_cast 强制类型转换
― 主要用于类层次间的转换,还可以用于类之间的交叉转换
―dynamic_cast 具有类型检查的功能,比static_cast更安全
小结
C++ 中内置了动态内存分配的专用关键字
C++ 中的动态内存分配是基于类型进行的
C++ 中命名空间概念用于解决名称冲突问题
C++ 细化了C 语言中强制类型转换的方式
―C++ 不推荐在程序中使用强制类型转换
―C++ 建议在强制类型转换时考虑一下究竟希望什么样的转换