C++新的关键字_6

一。动态内存分配

  1.C++中的动态内存分配

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

    -C++中的动态内存申请是基于类型进行的

    -delete关键字用于内存释放

  变量申请:

    Type* pointer = new Type

    delete pointer;

  数组申请:

    Type* pointer = new Type[N];

    delete[] 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;
}

二.new 与 malloc 函数的区别

 1.  new关键字是C++的一部分,malloc是由C库提供的函数

 2.  new以具体类型为单位进行内存分配,malloc只能以字节为单位进行内存分配

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

  pi = 1; pf = 2.0; pc = 'c';

三。C++中命名空间

  1.在C语言中只有一个全局作用域

    C语言中所有的全局标识符共享一个作用域

    #标识符之间可能发生冲突

  2.C++提出了命名空间的概念

    #命名空间将全局作用域分为不同的部分

    #不同命名空间中的标识符可以同名而不会发生冲突

    #命名空间可以相互嵌套

    #全局作用域也叫默认命名空间

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

三。强制类型转换

  1.C语言中的强制类型转换

   错误程序

  

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

  2.C方式强制类型转换存在的问题

  #过于粗暴

    任意类型之间都可以进行转换、编译器很难判断其正确性

  #难以定位

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

  在程序设计理论中强制类型转换是不被推荐的,和goto语句一样,应该尽量避免。

  3.如何进行可靠的强制类型转换

  C++将强制类型转换分为4中不同的类型

  a. static_cast  b.const_cast

  c. dynamic_cast  d.reinterpret_cast

  a。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是编译其进行转换,无法在运行时检测类型。

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

   c.reubterpret_cast 强制类型转换

   #用于指针类型间的强制转换

   #用于整数和指针类型间的强制转换

#include <stdio.h>

int main()
{
    int i = 0;
    char c = 'c';
    int* pi = reinterpret_cast<int*>(&c);       //指针用这个转换 
    char* pc =  reinterpret_cast<char*>(&i);
    
    c = static_cast<char>(i); 
    
    printf("Press any key to continue...");     //整数用这个转换 
    getchar();
    return 0;
}

  4.dynamic_cast强制类型转换

   #主要用于类层次之间的转换,还可以用于类之间的交叉转换

     #dynamic_cast具有类型检查的功能,比如static_cast更安全。

四。小结

  1. c++中内置了动态内存分配的专用关键字(new,delete)

  2. c++中的动态内存分配是基于类型进行的

  3. c++中命名空间概念用于解决名称冲突问题

  4.c++细化了C语言中强制类型转换的方式

   a。C++不推荐在程序中使用强制类型转换

   b。c++建议了在强制类型转换时考虑一下究竟希望什么样的转换。 

原文地址:https://www.cnblogs.com/lvxiaoning/p/7505806.html