Calling members of null pointers-to-struct

BCS ao at pathlink.com
Sun May 6 15:22:41 PDT 2007


Reply to Tor,

> Why is this illegal in D?  As I understand it (from discussion with
> GregorR on IRC), methods of a D struct are not virtual --- the
> following program cannot use vtables as an excuse for its AssertError.
> 
> struct foo {
> int doit(int x) { return 42; }
> }
> void main() {
> foo *bar;  bar.doit(9);
> }

try this

> foo *bar= cast(foo*)(null+1);  bar.doit(9);

I was found this trick because I had a function that needed to be called 
as a delegate and I didn't want the overhead of doing the nested call trick.

p.s.

Wow, 2 (strange things I was working on that other people bring up) in one 
day! The other is label variables.





More information about the Digitalmars-d mailing list