invalid offsetof from non-POD type

Matthias Benkmann matthias@winterdrache.de
Fri Apr 25 23:30:00 GMT 2003


On Thu, 24 Apr 2003 21:09:23 -0700 John Quigley <johnw@lowestplane.org>
wrote:

> Perhaps it would be useful if I described how the code that I'm working
> on uses offsetof.  It is the Torque game engine from
> www.garagegames.com.  The engine has an internal c-like scripting
> language.  The fields and methods of the c++ classes in the engine can
> be exposed to this scripting language.  For each field, the engine uses
> offsetof to compute the offset of the field in the class.  Later on,
> when the scripts read or write to the field, the script runtime locates
> the field by adding the offset to the base pointer of the object in
> question.

Can't you wrap the POD-stuff in a real POD like this:

#include <cstddef>
#include <iostream>  
using namespace std;

class Foo   
{
   public:
      struct foodata{
        int x;
        char fillerdata[256];
        int y;
      } data;
  
      Foo()
      {        
         data.x = 0;
         data.y = 0;
      }
};

int main()
{
   int yoffset = offsetof(Foo::foodata, y);
   cout << "yoffset: " << yoffset << endl;

   Foo foo;
   
   // get a pointer to y using the computed offset
   int* yptr = (int*)(((unsigned char*)&foo.data) + yoffset);
   // test it be assigning 
   *yptr = 15;
   cout << "foo values: " << foo.data.x << ", " << foo.data.y << endl;

   return 0;
}
 

MSB

-- 
Support bacteria - they're the only culture some people have.



More information about the Gcc mailing list