C++: reinterpret cast

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • A

    C++: reinterpret cast

    Hi,

    I have not been able to find an example for using the reinterpret cast. Any
    ideas?


    Regards,
    A



  • Colin

    #2
    Re: C++: reinterpret cast

    "A" <A@iprimus.com. au> writes:
    [color=blue]
    > Hi,
    >
    > I have not been able to find an example for using the reinterpret cast. Any
    > ideas?[/color]

    I don't think you'll find very many of them because it's such a bad
    idea to use them in the first place.

    --
    These largely uninformed opinions are not those of my employer
    in any way, shape, or form.

    Comment

    • .oO LGV Oo.

      #3
      Re: reinterpret cast


      "A" <A@iprimus.com. au> a écrit dans le message de
      news:3fbcb081_1 @news.iprimus.c om.au...[color=blue]
      > Hi,
      >
      > I have not been able to find an example for using the reinterpret cast.[/color]
      Any[color=blue]
      > ideas?
      >
      >
      > Regards,
      > A[/color]

      here's a small and very simple example of up/down-casting :

      class A
      {
      };

      class B : public A
      {
      };

      where an A is awaited, you can pass a B since B is a specialisation; ex :

      void fct(A *)
      {
      }

      B *pB = new B();
      fct(pB);

      on the other, where a B is expected, you *may* pass a A, with the use of the
      reinterpret_cas t :

      void fct2(B *);

      A *pA = new A();
      fct2(pA); // compiler error, polymorphism does not apply with downcasting
      fct(reinterpret _cast<B *>(pA)); // force the compiler to do what you want

      of course, with such things, you'd better be sure or what you're doing... ie
      if the code of fct2 uses an attribute of B that does not exist in A, you may
      get some undefined behaviour (?) or maybe crash (?) (I let those who know
      the standard by heart clarify this point...)

      btw, you can always use reinterpret_cas t for casting any pointers ; ie
      you're dealing with a 32 bits integers array, and you want to change a
      byte... here's come a reinterpret_cas t + offset...


      Comment

      • Karl Heinz Buchegger

        #4
        Re: reinterpret cast



        ".oO LGV Oo." wrote:[color=blue]
        >
        > on the other, where a B is expected, you *may* pass a A, with the use of the
        > reinterpret_cas t :
        >
        > void fct2(B *);
        >
        > A *pA = new A();
        > fct2(pA); // compiler error, polymorphism does not apply with downcasting
        > fct(reinterpret _cast<B *>(pA)); // force the compiler to do what you want
        >[/color]

        On the other hand you can also do:

        double* pC;
        fct( reinterpret_cas t< B* >( pC ) );

        :-)

        Here is what Microsoft has to say about reinterpret_cas t

        <Quote>
        The reinterpret_cas t operator allows any pointer to be converted into any other pointer
        type. It also allows any integral type to be converted into any pointer type and vice
        versa. Misuse of the reinterpret_cas t operator can easily be unsafe. Unless the desired
        conversion is inherently low-level, you should use one of the other cast operators.

        The reinterpret_cas t operator can be used for conversions such as char* to int*, or
        One_class* to Unrelated_class *, which are inherently unsafe.

        The result of a reinterpret_cas t cannot safely be used for anything other than being
        cast back to its original type. Other uses are, at best, nonportable.
        </Quote>

        --
        Karl Heinz Buchegger
        kbuchegg@gascad .at

        Comment

        • .oO LGV Oo.

          #5
          Re: reinterpret cast


          "Karl Heinz Buchegger" <kbuchegg@gasca d.at> a écrit dans le message de
          news:3FBCD7C3.3 112CA8C@gascad. at...[color=blue]
          >
          >
          > ".oO LGV Oo." wrote:[color=green]
          > >
          > > on the other, where a B is expected, you *may* pass a A, with the use of[/color][/color]
          the[color=blue][color=green]
          > > reinterpret_cas t :
          > >
          > > void fct2(B *);
          > >
          > > A *pA = new A();
          > > fct2(pA); // compiler error, polymorphism does not apply with[/color][/color]
          downcasting[color=blue][color=green]
          > > fct(reinterpret _cast<B *>(pA)); // force the compiler to do what you[/color][/color]
          want[color=blue][color=green]
          > >[/color]
          >
          > On the other hand you can also do:
          >
          > double* pC;
          > fct( reinterpret_cas t< B* >( pC ) );
          >
          > :-)[/color]

          yep, in fact we can do almost what we want with the reinterpret_cas t :) and
          THIS IS the trap.... I just wanted to show a common use of this kinda cast
          for downcasting ; anyway, one should be careful and aware of what's going
          on...


          Comment

          • Jonathan Hoyle

            #6
            Re: C++: reinterpret cast

            > I have not been able to find an example for using the reinterpret cast. Any[color=blue]
            > ideas?[/color]

            The best one I can think of involves using a predefined API which you
            wish to use a long integer parameter to hold a pointer. For example,
            Photoshop's main entry point into their DLL's have a long that is
            expected to be a pointer holder for you. Why not just call it a void
            *? Well, in the ugly old days of Windows 3.1, pointers were 16-bits,
            whereas on the Mac they've always been 32-bit. So that parameter
            sizes remain cross-platform, Adobe used an integer value, which
            reinterp_cast'i ng was meant for.

            Jonathan Hoyle
            Gene Codes Corporation

            Comment

            Working...