Offset of mixin

O

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, 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.
 
O

Ole Nielsby

Alf P. Steinbach said:
* 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,006
Messages
2,570,265
Members
46,861
Latest member
SanoraS48

Latest Threads

Top