很少有人在C ++中铸造操作符 [英] Few doubts about casting operators in C++

查看:97
本文介绍了很少有人在C ++中铸造操作符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道的 reinterpret_cast 可以将任何指针类型转换为任何其他指针类型。关于这个转换操作符我想问的问题是:


  1. 如何reinterpret_cast

  2. 如何在使用 reinterpret_cast 时确保安全性?据我所知,它不能保证安全铸造,那么在使用 reinterpret_cast时需要注意什么?

  3. 什么是这个操作符的实际用法。我没有真正遇到它在我的专业编程经验,其中我不能绕过没有使用这个运算符。除了通常的int *到char *之外的任何实践示例将是非常有帮助和赞赏。

另一个关于铸造操作符的问题:

铸造操作符( static_cast dynamic_cast const_cast reinterpret_cast $ c>运算符即是最好的我的理解,所以是正确的语句,使铸造运算符不能被重载不像大多数其他运算符 (我知道,并不是所有的运营商都可以超载,我知道哪些不能(除了QI问,请避免燃烧我的)。只是我有这个疑问,因为他们是运营商,标准说什么关于这些?

解决方案


  1. 没有魔法。 reinterpret_cast 通常只是(至少尝试)处理你在这个地址找到的,如果它是我指定的类型。

  2. 在某些情况下,你可以从像歧视的联盟这样的事情中获得安全。例如,如果你正在读取网络数据包,并且阅读足够看到你收到的是一个TCP数据包,那么你可以(公平地)安全地做一个 reinterpret_cast IPHdr TCPHdr (或任何您刚刚使用的名称)。编译器不会(再次,通常)做很多事情 - 任何安全是由你来实施和强制执行。
  3. 我使用的代码,像我在2)描述,


  4. 对于最后一个问题:您可以为一个类重载转换:

      class XXX {
    public:
    operator YYY(){return whatever; }
    };

    这可以用于一般的转换 - 无论是由static_cast,C风格的,甚至是隐式转换。 C ++ 0x允许添加一个显式限定符,因此它不会用于隐式转换,但仍然没有办法区分static_cast和C风格


    The reinterpret_cast as we know can cast any pointer type to any another pointer type. The question I want to ask regarding this cast operator are:

    1. How does reinterpret_cast work, What is the magic(the internal implementation) that allows reinterpret_cast to work?
    2. How to ensure safety when using reinterpret_cast? As far as i know, it doesn't guarantee of safe casting, So what precaution to take while using reinterpret_cast?
    3. What is the practical usage of this operator. I have not really encountered it in my professional programing experience, wherein I could'nt get around without using this operator.Any practical examples apart from usual int* to char* will be highly helpful and appreciated.

    One other Question regarding casting operators in general:
    Casting operators(static_cast, dynamic_cast, const_cast, reinterpret_cast) are all called Operators i.e is to the best of my understanding, So is it correct statement to make that casting operators cannot be overloaded unlike most other operators (I am aware not all operators can be overloaded and I am aware of which can't be(except the Q I am asking, Please refrain flaming me on that) Just I had this doubt that since they are operators, what does the standard say about these?

    解决方案

    1. There is no magic. reinterpret_cast normally just means (at least try to) treat what you find at this address as if it was the type I've specified. The standard defines little enough about what it does that it could be different from that, but it rarely (if ever) really is.
    2. In a few cases, you can get safety from something like a discriminated union. For example, if you're reading network packets, and read enough to see that what you've received is a TCP packet, then you can (fairly) safely do a reinterpret_cast from IPHdr to TCPHdr (or whatever names you happen to have used). The compiler won't (again, normally) do much though -- any safety is up to you to implement and enforce.
    3. I've used code like I describe in 2), dealing with different types of network packets.

    For your final question: you can overload casting for a class:

    class XXX { 
    public:
        operator YYY() { return whatever; }
    };
    

    This can be used for conversions in general though -- whether done by a static_cast, C-style cast, or even an implicit conversion. C++0x allows you to add an explicit qualifier so it won't be used for implicit conversions, but there's still no way to differentiate between a static_cast and a C-style cast though.

    这篇关于很少有人在C ++中铸造操作符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆