Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Offset of mixin

Reply
Thread Tools

Offset of mixin

 
 
Ole Nielsby
Guest
Posts: n/a
 
      07-08-2008
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.


 
Reply With Quote
 
 
 
 
Ole Nielsby
Guest
Posts: n/a
 
      07-08-2008
Alf P. Steinbach <> wrote:
>* 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparing offset-aware and offset-naive datetimes? Roy Smith Python 4 01-27-2013 03:17 PM
A (unpythonic) pythonable mixin recipe. Paolino Python 0 08-16-2005 02:38 PM
MixIn method to call the method it overrides: how? Mac Python 1 06-18-2005 02:30 PM
Translated Offset to Source Offset Lance Riedel XML 2 10-15-2003 03:04 PM
mixin class Udo Gleich Python 5 07-31-2003 03:42 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57