POV-Ray : Newsgroups : povray.binaries.programming : [C++] Question about reinterpret_cast<> : Re: [C++] Question about reinterpret_cast<> Server Time
19 Apr 2024 09:32:22 EDT (-0400)
  Re: [C++] Question about reinterpret_cast<>  
From: Warp
Date: 1 Feb 2002 07:45:15
Message: <3c5a8ddb@news.povray.org>
Tim Chan <tim### [at] blueyondercouk> wrote:
: #include <iostream>
: using namespace std;
: const int sz = 100;

: struct X { int a[sz]; };

: void print(X* x) {
:   for(int i = 0; i < sz; i++)
:     cout << x->a[i] << ' ';
:   cout << endl << "--------------------" << endl;
: }

: int main() {
:   X x;
:   print(&x);

:   int* xp = reinterpret_cast<int*>(&x);

:   for(int* i = xp; i < xp + sz; i++)
:     *i = 0;

:   print(reinterpret_cast<X*>(xp));

: } ///:~

  I know I'm replying to a rather old message, but this code looked so
horrible, that I must reply.
  Firstly I think that it breaks the standard. One cannot assume that
the member variables of a struct start right at the beginning of the
struct.
  Secondly, why not just:

int main() {
  X x;
  print(&x);

  for(int i = 0; i < sz; ++i)
    x.a[i] = 0;

  print(&x);
}

  It's a lot nicer, doesn't use hacks and doesn't break the standard.

-- 
#macro N(D)#if(D>99)cylinder{M()#local D=div(D,104);M().5,2pigment{rgb M()}}
N(D)#end#end#macro M()<mod(D,13)-6mod(div(D,13)8)-3,10>#end blob{
N(11117333955)N(4254934330)N(3900569407)N(7382340)N(3358)N(970)}//  - Warp -


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.