Pointer to C++ member possible?

Bill Baxter dnewsgroup at billbaxter.com
Sun Mar 9 14:03:46 PDT 2008


Jarrett Billingsley wrote:

>> But seems like lack of a similar concept in D is a problem.  The topic of 
>> C++-like pointer-to-member has come up before.  I think there is some sort 
>> of hack to do a D version of it by setting the delegate's .ptr property? 
>> Casting to void* and setting it seems to work:
> 
> Works for pointers-to-member-functions (that is, if the given C++ ABI uses 
> the same ABI as a delegate), but what about pointers-to-member-data? 

Is there such a thing in C++?  AFAIK pointer-to-member-data is just the 
same thing as "pointer-to-data" in both C++ and D.  Anyway if c++ does 
have it, I think you could fake it with .offsetof and casts.
( http://www.digitalmars.com/d/1.0/class.html )

However, .offsetof seems broken at the moment.  This example for the 
docs doesn't compile for me:

class Foo
{
     int x;
}
...
void test(Foo foo)
{
     size_t o;

     o = Foo.x.offsetof;   // yields 8
}

With dmd 1.028 I get:

ptrmemfun.d(29): Error: 'this' is only allowed in non-static member 
functions, not test
ptrmemfun.d(29): Error: this for value needs to be type Foo not type int


--bb


More information about the Digitalmars-d-learn mailing list