Calling members of null pointers-to-struct

James Dennett jdennett at acm.org
Sun May 6 14:23:45 PDT 2007


Tor Myklebust wrote:
> 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.

[snip]

> (Except for one minor detail about foo not being initialised to null by 
> default, this is perfectly good C++ code; calling a nonvirtual method of 
> a null pointer-to-struct simply results in the 'this' parameter coming 
> out to be null.  So, like, why doesn't D do the same?)

It's technically invalid in C++ also unless the function
is a static member function.  The fact that most compilers
allow it to run means nothing; in a portably valid C++
program, the value of the "this" pointer is never null.

If D requires this to be diagnosed in at least some modes,
that would be a minor improvement on C++ (which leaves
this as undefined, largely for performance reasons).

-- James



More information about the Digitalmars-d mailing list