471,070 Members | 1,494 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,070 software developers and data experts.

Offset of mixin

Given these 3 classes

class A {virtual void a(){}};
class B {virtual void b(){}};
class C: public A, public B {};

I want the offset of B in C, as a size_t value, and preferably as a
constant expression.

I got a solution that seems to work on VC9Express:

(char *)&(B&)*(A*)auxp - (char *)auxp

where auxp is some irrelevant non-null ptr value (using NULL
results in all zero offsets) ... but it seems messy and the sample
below indicates that it gets computed at runtime in VC though
the offset is obviously a constant.

I wonder if there is an easy and standard-compliant way
to get it?

(In case you wonder what it's for: I'm binding my PILS language to
the Juce framework which uses mixin classes for many purposes. I
do typecasting by means of tables that need this offset.)

This sample
---------
class A {virtual void a(){}};
class B {virtual void b(){}};
class C: public A, public B {};
int main(){
char vux[(char *)&(A&)*(A*)&main - (char *)&main];
return 0;
}
------compiles with MINGW/C++ at
http://www.dinkumware.com/exam/default.aspx
but not with VS, VS doesn't recognize the expression as a constant
expression.
Jul 8 '08 #1
1 1291
Alf P. Steinbach <al***@start.nowrote:
>* Ole Nielsby:
>Given these 3 classes

class A {virtual void a(){}};
class B {virtual void b(){}};
class C: public A, public B {};

I want the offset of B in C [...]

I wonder if there is an easy and standard-compliant way
to get it?

Nope. Only for PODs, then via offsetof macro.
>([...] I do typecasting by means of tables [...])

Do it some other way.
I'll probably stick with what I've got - it is fast and covers my needs.
Better, avoid the typecasting.
Hardly possible, considering the nature of my project. PILS is
dynamically typed and Juce sometimes requires passing a mixin
as a parameter. Given an object that implements this mixin, I need
to be able to cast it dynamically.
Cheers, & hth.,
It didn't help with my coding, but I feel less stupid now, knowing I
haven't overlooked "the obvious solution" that isn't there.
Jul 8 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by zimba | last post: by
5 posts views Thread by Udo Gleich | last post: by
reply views Thread by Paolino | last post: by
reply views Thread by barnesc | last post: by
6 posts views Thread by Alex Hunsley | last post: by
3 posts views Thread by Ed Leafe | last post: by
2 posts views Thread by ish | last post: by
1 post views Thread by Scott David Daniels | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.