Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 8th, 2008, 10:35 PM
Ole Nielsby
Guest
 
Posts: n/a
Default 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.


  #2  
Old July 9th, 2008, 12:27 AM
Ole Nielsby
Guest
 
Posts: n/a
Default Re: Offset of mixin

Alf P. Steinbach <alfps@start.nowrote:
Quote:
>* Ole Nielsby:
Quote:
>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.
>
Quote:
>([...] 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.
Quote:
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.
Quote:
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.


 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles