新的关键字

来源:互联网 发布:淘宝网减肥药 编辑:程序博客网 时间:2024/06/18 07:30

动态内存分配:

c++中通过new关键字进行动态内存申请;

c++中的动态内存申请基于类型进行的;

delete关键字用于内存释放;


c++中的动态内存分配:

#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不具备内存初始化的特性;

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++中命名空间的定义:

#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++中命名空间的使用:

#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);
    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)

存在的问题:

过于粗暴:任意类型之间都可以进行转换,编译器难以判断其正确性;

难于定位:在源码中无法快速定位所有使用强制类型转换的语句;

在程序设计理论中,强制类型转换语句不被推荐使用,与goto语句一样,应当尽量避免使用。



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;
}

static_cast转换是编译期进行转换的,无法在运行时检测类型;所以类类型之间的转换可能存在风险;

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\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!
    printf("Press any key to continue...");
    getchar();
    return 0;
}

dynamic_cast强制类型转换:

主要用类层次之间的转换,也可用于类之间的强制类型转换;

dynamic_cast具有类型检查功能,比static_cast更安全;

C++中的动态内存分配是基于类型进行的
C++中内置了动态内存分配的专用关键字
C++中命名空间概念用于解决名称冲突问题
C++细化了C语言中强制类型转换的方式
C++不推荐在程序中使用强制类型转换
C++建议在强制类型转换时考虑一下究竟希望什么样的转



0 0