Explicit default constructor for structs

Jonathan M Davis jmdavisProg at gmx.com
Fri Apr 11 10:16:13 PDT 2014


On Saturday, April 12, 2014 02:26:54 Daniel Murphy wrote:
> "monarch_dodra"  wrote in message
> news:ktbdlcaqahsqmwkiddvc at forum.dlang.org...
> 
> > Why? The D version would just go from "object.Error Access Violation" to
> > "AssertError null this".
> > 
> > And in release, you'd still (highly probably) still get the Access
> > Violation.
> 
> Sadly I've encountered C++ code that deliberately calls member functions on
> null class pointers.

I've never seen code that it did it on purpose, but I've certainly seen code 
that did it on accident. And what's really fun is when it's non-virtual, 
because that won't segfault on the call - only when it does something inside 
the function which causes the this pointer to be dereferenced (e.g accessing a 
member variable or virtual member function). _That_ can really confuse people 
when it happens. At one point at work, we had a class that was never 
instantiated but worked great (it really didn't need to be a class at all, 
just free functions, as it didn't actually have any member variables). 
Fortunately, I'd seen the problem before and could explain to the others once 
I remembered, but no one else on the team had ever seen anything like it and 
all found it quite surprising. _I_ was certainly shocked by it when I 
originally ran into it - though it all makes sense when you look at what the 
compiler needs to do when calling a function.

- Jonathan M Davis


More information about the Digitalmars-d mailing list