this() not executing code on structs

Steven Schveighoffer schveiguy at yahoo.com
Wed Oct 21 14:59:47 PDT 2009


On Wed, 21 Oct 2009 17:54:08 -0400, grauzone <none at example.net> wrote:

> I'd really like to know why "scope x = new X();" is "unsafe", while  
> encouraging doing exactly the same with structs seems to be a perfectly  
> fine idea. Allocating structs on the stack is obviously not any safer  
> than with classes. I don't remember the exact reasons why you wanted to  
> turn "scope" into a library feature, but I think I remember something  
> about discouraging it for safety reasons; please forgive me is this is  
> wrong.

A class is a reference type.  If you pass the reference to the stack to a  
function that then stores it for later use, it is unsafe.  If you return  
it from the function, it's unsafe.  If you do the same with a struct (not  
a struct pointer), this is not the case.  Note that you *could* have the  
same problem with struct pointers or references, but typically, you expect  
to treat struct references like they are referencing stack data, it's not  
typical for classes.

-Steve



More information about the Digitalmars-d mailing list