第5课 - 新的关键字
1. 动态内存分配
C++中通过new关键字进行动态内存申请,C++中的动态内存申请是基于类型进行的,delete关键字用于内存释放。
变量申请:
Type* pointer = new Type;
//......
delete pointer;
数组申请:
Type* pointer = new Type[N];
//......
delete[N] pointer;
程序:C++中的动态内存分配
#include <stdio.h>
int main()
{
int* p = new int;
*p = 5;
*p = *p + 10;
printf("p = %p ", p);
printf("*p = %d ", *p);
delete p;
p = new int[10];
for(int i=0; i<10; i++)
{
p[i] = i + 1;
printf("p[%d] = %d ", i, p[i]);
}
delete[] p;
printf("Press any key to continue...");
getchar();
return 0;
}
l 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 ", *pi);
printf("*pf = %f ", *pf);
printf("*pc = %c ", *pc);
delete pi;
delete pf;
delete pc;
printf("Press any key to continue...");
getchar();
return 0;
}
2. C++中的命名空间
在C语言中只有一个全局作用域,C语言中所有的全局标示符共享同一个作用域,表示符之间可能发生冲突。
C++中提出了命名空间的概念,命名空间将全局作用域分成不同的部分,不同命名空间中的标示符可以同名而不会发生冲突。命名空间可以相互嵌套,全局作用域也叫默认命名空间。
程序:C++命名空间的定义
#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;
}
l C++命名空间的使用:
使用整个命名空间:using namespace name;
使用命名空间中的变量:using nama::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 ", i);
printf("i = %d ", Second::i);
P p = {2, 3};
printf("p.x = %d ", p.x);
printf("p.y = %d ", p.y);
printf("Press any key to continue...");
getchar();
return 0;
}
3. 强制类型转换
(1)C方式的强制类型转换(Type)(Expression) or Type(Expresssion)
#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 ", p->x);
printf("p->y = %d ", p->y);
printf("Press any key to continue...");
getchar();
return 0;
}
C方式强制类型转换存在的问题:
过于粗暴:任意的类型之间都可以进行转换,编译器很难判断正确性;难于定位:在源码中无法快速定位所有使用强制类型转换的语句。
在程序设计理论中强制类型转换不是被推荐的,与goto语句一样,应该尽量避免。
(2)C++中的强制类型转换
有四种类型:
static_cast
const_cast
dynamic_cast
reinterpret_cast
用法:xxx_cast<Type>(Expression)
l static_cast强制类型转换
用于基本类型间的转换,但是不能用于基本类型指针之间的转换;用于有继承关系类对象之间的转换和类型指针之间的转换。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);
printf("Press any key to continue...");
getchar();
return 0;
}
l const_cast
用于除去变量的const属性
#include <stdio.h>
int main()
{
const int& j = 1;
int& k = const_cast<int&>(j);
const int x = 2;
int& y = const_cast<int&>(x);
k = 5;
printf("k = %d ", k);
printf("j = %d ", j);
y = 8;
printf("x = %d ", x);
printf("y = %d ", y);
printf("&x = %p ", &x);
printf("&y = %p ", &y);
printf("Press any key to continue...");
getchar();
return 0;
}
l reinterpret_cast
用于指针类型间的强制转换,用于整数和指针类型之间的强制转换。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!
printf("Press any key to continue...");
getchar();
return 0;
}
l dynamic_cast
主要用于层次之间的转换,还可以用于类之间的转换;dynamic_cast具有类型检查的功能,比static_cast更安全。
小结:
C++中内置了动态内存分配的专用关键字,其中的动态内存分配是基于类型进行的。
C++中命名空间的概念用于解决名称冲突问题,C++细化了C语言中强制类型转换的方式。
C++不推荐在程序中使用强制类型转换,C++建议在强制类型转换的时候考虑一下究竟希望什么样的转换。