Re: Inheritance and offsetof
On Sep 22, 3:10 pm, Marcel M=FCller <news.5.ma...@spamgourmet.com>
wrote:
Francesco S. Carta wrote:
If you already have an instance of your type, you are
right, but otherwise you have to make your own
pseudo-instance. And that is exactly the way the macros
offsetof and my structoffsetof work with all that casts.
Furthermore when dealing with an instance I would prefer to
cast to char*: (char*)&b.m3 - (char*)&(A&)b
Thanks for your explanation, now it's clearer.
I just saw a bug in the example, which may have caused confusion.
It should have been:
void foo(A*);
int main()
{ B b;
b.m1 = offsetof(B, m3) - structoffsetof(B, A);
foo(&b);
return 0;
}
I still think you could avoid those macros, also you could
avoid C- style casts.
I think there is no way of implementing things like offsetof
without a C-style cast.
The most obvious way of implementing offsetof is something like:
#define offsetof( type, elem ) __builtin_offsetof( type, elem )
No casts, C-style or otherwise.
I'm just saying this because I've been told several times to
never use C-style casts,
This works unless you have to deal with void* or something
like offsetof.
No. The only thing a C-style cast can do that static_cast or
reinterpret_cast (combined with const_cast) can't is cast to a
private base class.
--
James Kanze