• 数据类型转换(static_cast)

    //数据类型转换
    printf("%d\n", static_cast<int>(10.2));
  • 指针类型转换(reinterpret_cast)
     指针类型转换
    int *pint = new int();
    char *pch = reinterpret_cast<char *>(pint);
  • 涉及到const的指针类型转换(const_cast)
     const int num[] = { ,,,, };
    const int *p = num;
    int *pint = const_cast<int *>(p);
  • 子类转化为父类(dynamic_cast)
     class man
    {
    public:
    int name;
    //加上virtual关键字,可以使得父类用子类初始化后可以调用子类的函数
    virtual void run()
    {
    cout << "man is running" << endl;
    }
    }; class son :public man
    {
    public:
    void run()
    {
    cout << "son is running" << endl;
    }
    }; void main()
    {
    /*man man1;
    son son1;
    man *p = &man1;
    p->run();
    p = &son1;
    p->run();*/
    man *pman = new man;
    son *pson = new son;
    //子类指针转换为父类指针,但是还是调用子类的函数
    man *pfu = dynamic_cast<man *>(pson);
    pfu->run();
    system("pause");
    }

43.c++指针类型转换的更多相关文章

  1. C语言中将0到1000的浮点数用强制指针类型转换的方式生成一幅图像

    搞过计算机图像的人都知道,图像中的每一个像素通常为一个整型数,它可以分成4个无符号的char类型,以表示其RGBA四个分量.一幅图像可以看做是一个二维整型数组.这里我会生成一个float数组,其数组大 ...

  2. c++ 指针类型转换

    1.数据类型转换(static_cast) //数据类型转换printf("%d\n", static_cast<int>(10.2)); 2.指针类型转换(reint ...

  3. C指针类型转换问题

    先看下面的代码: #include<stdio.h> int main () { int a; char *x; x = (char *) &a; a = 512; x[0] = ...

  4. C++_知识点_指针类型转换

    #include <iostream> using namespace std; int main(){ ] = {, , , , , , , , , }; int* p = (int*) ...

  5. C++智能指针类型转换

    #include <iostream> #include <memory> struct Base { int a; virtual void f() const { std: ...

  6. [翻译]类型双关不好玩:C中使用指针重新解释是坏的

    原文地址 Type punning isn't funny: Using pointers to recast in C is bad. C语言中一个重新解释(reinterpret)数据类型的技巧有 ...

  7. 对象布局已知时 C++ 对象指针的转换时地址调整

    在我调试和研究 netscape 系浏览器插件开发时,注意到了这个问题.即,在对象布局已知(即对象之间具有继承关系)时,不同类型对象的指针进行转换(不管是隐式的从下向上转换,还是强制的从上到下转换)时 ...

  8. 【面经】【转】C++类型转换

    C风格的强制类型转换(Type Cast)很简单,不管什么类型的转换统统是:type b = (type) a. C++风格的类型转换提供了4种类型转换操作符来应对不同场景的应用. const_cas ...

  9. C++ 四种强制类型转换

    来自csdn:http://blog.csdn.net/hgl868/article/details/46619399 C风格的强制类转换(Type Cast)很简单,不管什么类型的转换统统是: TY ...

随机推荐

  1. 洛谷 P3467 [POI2008]PLA-Postering

    P3467 [POI2008]PLA-Postering 题目描述 All the buildings in the east district of Byteburg were built in a ...

  2. linux虚拟机拓展大小

    http://blog.csdn.net/wutong_login/article/details/40147057?utm_source=tuicool http://www.linuxidc.co ...

  3. jquery快速清除复选框、单选框的选中

    $(":checked").attr("checked", "");

  4. java枚举在android项目应用

    今天修复一个公司非常早曾经的android应用功能,里面的代码逻辑已经全然错乱,然后发现返回的数据全然不正确了.然后修复了整整两天.然后我又一次整理了一遍,重构就算不上了. 然后就用上了枚举. 什么是 ...

  5. vector和list容器之间的复制

    #include <iostream> #include <list> #include <string> #include <vector> #inc ...

  6. Thumb指令集与ARM指令集的差别

    Thumb指令集          Thumb指令能够看做是ARM指令压缩形式的子集.是针对代码密度[1]的问题而提出的.它具有16为的代码密度.Thumb不是一个完整的体系结构,不能指望处理程序仅仅 ...

  7. poj2002 哈希

    Squares Time Limit: 3500MS   Memory Limit: 65536K Total Submissions: 17666   Accepted: 6735 Descript ...

  8. Cisco交换机端口安全

    Cisco交换机端口安全       通过端口设置,可以限制允许访问交换机上某个端口的MAC地址以及IP(可选)来实现严格控制对该端口的输入,最终确保网络接入安全.配置网络安全时应该注意如下问题: 1 ...

  9. 由安装两块网卡的linux系统中引起网络不通想到的

    由安装两块网卡的linux系统中引起网络不通想到的 一天,小王突然急匆匆的来找我,他说:"我在机子上刚装的redhat怎么老也ping不通服务器,我网卡的驱动都安装了,ping 自己的两块网 ...

  10. ES6学习笔记(一)新的变量定义命令let和const

    1.一些历史 ES6(ECMAScript 6.0)是 JavaScript 语言的新一代标准,于2015 年 6 月正式发布,距今已经4年了,它的目标,是使得 JavaScript 语言可以用来编写 ...