From 3989623712584827855
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,28671cb074853115
X-Google-Attributes: gidf78e5,public
From: phalpern@truffle.ma.ultranet.com (Pablo Halpern)
Subject: Re: offsetof macro, can it be used in non-trivial classes?
Date: 1996/12/27
Message-ID: <32c50648.4321059@news.ma.ultranet.com>#1/1
X-Deja-AN: 206252322
x-nntp-posting-host: truffle.ultranet.com
references: <01bbead3$6fbf55f0$c07797cd@hacknowledge> <VA.0000000f.000b8656@enterprise>
content-length: 1076
content-type: text/plain; charset=us-ascii
organization: UltraNet Communications, Inc.
mime-version: 1.0
newsgroups: comp.std.c++
originator: clamage@taumet


Jean-Louis Leroy <100611.1330@compuserve.com> wrote:

><< How reliable is the offsetof macro when used on a complex class under MI? >>
>
>As the others say, it's a bad idea. In most situations, pointers to data members 
>will do the job better.

The problem with pointers to data members is that they only work one
way. As far as I know, there is no portable solution to the following
problem:

  // Assume bar is not a POD, so foo is not a POD
  struct foo : public bar { int x, y, z; };

  int f(int *p)
  {
    // Problem: If we know that p is the y member of a foo object,
    // get the address of the entire foo object
  }

The above problem would be solvable if &foo::y could be applied
backwards. It would also be solvable using offsetof() if foo were a POD.
Fortunately, there are very few situations like this that couldn't be
fixed by using a better design.

-------------------------------------------------------------
Pablo Halpern                   phalpern@truffle.ultranet.com

I am self-employed. Therefore, my opinions *do* represent 
those of my employer.


[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]



