Offset of mixin

Discussion in 'C++' started by Ole Nielsby, Jul 8, 2008.

  1. Ole Nielsby

    Ole Nielsby Guest

    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.
     
    Ole Nielsby, Jul 8, 2008
    #1
    1. Advertising

  2. Ole Nielsby

    Ole Nielsby Guest

    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.
     
    Ole Nielsby, Jul 9, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lance Riedel

    Translated Offset to Source Offset

    Lance Riedel, Oct 14, 2003, in forum: XML
    Replies:
    2
    Views:
    503
    Patrick TJ McPhee
    Oct 15, 2003
  2. Udo Gleich

    mixin class

    Udo Gleich, Jul 28, 2003, in forum: Python
    Replies:
    5
    Views:
    451
    Jeremy Bowers
    Jul 31, 2003
  3. Mac
    Replies:
    1
    Views:
    594
  4. Paolino
    Replies:
    0
    Views:
    283
    Paolino
    Aug 16, 2005
  5. Roy Smith
    Replies:
    4
    Views:
    277
    Roy Smith
    Jan 27, 2013
Loading...

Share This Page