zoukankan      html  css  js  c++  java
  • c++ 从int*转int

    参考:https://www.zhihu.com/question/421767822/answer/1481328334

    (从 int *int的转换损失精度)

    做了如下测试:

    class A {
        public:
            A() {}
    
            A(const A* pa) {
                a[0] = pa->a[0];
            }
            int a[8];
    };
    
    int main() {
        A *a = new A();
        a->a[0] = 2;
        printf("a:%ld, %ld
    ", a, &a);
        // auto aa = reinterpret_cast<A>(a); // error
        // A aa = (A)(a);
        A aa = static_cast<A>(a);
        cout<<aa.a[0]<<endl;
    auto bb = reinterpret_cast<size_t>(a); // bb是size_t类型
    }

    类对象指针强转为类对象,需要类里面实现这种转换的构造函数,否则会报错。不过即使实现了这类构造函数也不能用reinterpret_cast,要用static_cast。

    可以把指针类型转换为长整型。

  • 相关阅读:
    Python文件的两种用途
    模块的搜索路径
    循环导入问题
    import和from...import
    模块的四种形式
    函数小结
    面向过程编程
    内置函数
    匿名函数
    递归
  • 原文地址:https://www.cnblogs.com/starRebel/p/14202467.html
Copyright © 2011-2022 走看看