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