新的关键字
1. C++中的动态内存分配:C语言中malloc是C库的内容,不是C语言中的组成。C++中的new和delete是语言本身的关键字
.C++中通过new关键字进行动态内存分配
.C++中的动态内存申请是基于类型进行的
.delete关键字用于内存释放
变量申请:Type* pointer = new Type;……delete pointer;
数组申请:Type* pointer = new Type[N];……delete[] pointer;
#include <stdio.h>
int main()
{
int* p = new int;
*p = 5;
*p = *p + 10;
printf("p = %p\n", p);
printf("*p = %d\n", *p);
delete p;
p = new int[10];
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不具备内存初始化的特性
#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;
}
2.C++中的命名空间
在C语言中只有一个全局作用域,所有的全局标识符共享一个作用域,标识符之间可能发生冲突
C++中提出了命名空间的概念
.命名空间将全局作用域分成不同的部分
.不同的命名空间中的标识符可以同名而不会发生冲突
.命名空间可以相互嵌套
.全局作用域也叫做默认命名空间
C++命名空间的定义:namespacename{……}
#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;
using Second::Internal::P;
printf("i = %d\n", i);//0
printf("i = %d\n", Second::i);//1
P p = {2, 3};
printf("p.x = %d\n", p.x);//2
printf("p.y = %d\n", p.y);//3
printf("Press any key to continue...");
getchar();
return 0;
}
3.强制类型转换:
C方式的强制类型转换:(Type)(Expression) or Type(Expression)
#include <stdio.h>
typedef void(PF)(int);
struct Point
{
int x;
int y;
};
int main()
{
int v = 0x12345;
PF* pf = (PF*)v;
char c = char(v);
pf(v);
Point* p = (Point*)v;
printf("p->x = %d\n", p->x);
printf("p->y = %d\n", p->y);
printf("Press any key to continue...");
getchar();
return 0;
}
能够编译通过,但是运行错误!!!
C方式强制转换存在的问题:
1.过于粗暴:任意类型之间都可以进行转换,编译器很难判断其正确性
2.难于定位:在源码中无法快速定位所有使用强制类型转换的语句
在程序设计理论中强制类型转换是不被推荐的,与goto一样,应该尽量避免。
程序BUG的几个方面:运算符优先级,多线程交互,强制类型转换,内存泄露,野指针
C++将强制类型转换分为4种不同的类型:static_cast const_cast dynamic_cast reinterpret_cast
用法:XXX _cast<Type>(Expression)
static_cast强制类型转换
-用于基本类型间的转换,但不能用于基本类型指针间的转换
-用于有继承关系类对象之间的转换和类指针之间的转换
#include <stdio.h>
int main()
{
int i = 0x12345;
char c = 'c';
int* pi = &i;
char* pc = &c;
c = static_cast<char>(i);
pc = static_cast<char*>(pi);//oops
printf("Press any key to continue...");
getchar();
return 0;
}
static_cast 是编译期进行转换的,无法在运行时检测类型,所以类型之间的转换可能存在风险(只比C语言安全一点点--》好查找定位)
const_cast强制类型转换:用于除去变量的const
#include <stdio.h>
int main()
{
const int& j = 1;//得到只读变量j
int& k = const_cast<int&>(j);//k成为变量
const int x = 2;//得到常量
int& y = const_cast<int&>(x);//编译器为常量分配内存地址,y就是内存地址的别名,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); // Oops!应该使用static_cast
printf("Press any key to continue...");
getchar();
return 0;
}
reinterpret_cast 直接从二进制位进行复制,是一种极不安全的转换
dynamic_cast强制类型转换:
-主要用于类层次的转换,还可以用于雷之间的交叉转换
-dynamic_cast具有类型检查的功能,比static_cast更安全
小结:
C++中内置了动态内存分配的专用关键字
C++中的动态内存分配是基于类型进行的
C++中命名空间概念用于解决名称冲突问题
C++细化了C语言中强制类型转换的方式:C++不推荐在程序中使用强制类型转换,建议转换时考虑一下希望什么样的转换