www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Methods for expanding class in another class/struct

reply IGotD- <nise nise.com> writes:
We know that classes are all reference typed so that classes must 
be allocated on the heap. However, this memory could be taken 
from anywhere so basically this memory could be a static array 
inside the class. This is pretty much what the scoped template 
does when allocating a class on the stack. In practice allocating 
a class inside another class could be done in similar fashion.

Do we have a good example showing how to expand a class inside 
another class?
Shouldn't be have a standard template similar to scoped, that 
statically allocates space inside the class? This template should 
be available in the standard library.
Sep 26 2020
parent reply IGotD- <nise nise.com> writes:
One thing that struck me looking at the source code of scoped, 
would scoped work inside a class and not only for stack 
allocations?
Sep 26 2020
parent reply k2aj <krzysztof.jajesnica gmail.com> writes:
On Saturday, 26 September 2020 at 10:26:15 UTC, IGotD- wrote:
 One thing that struck me looking at the source code of scoped, 
 would scoped work inside a class and not only for stack 
 allocations?
It does work, the problem is that scoped returns a Voldemort type, so you have to use typeof(scoped!SomeClass(someConstructorArgs)) to declare a field. Gets really annoying when doing it with any class that doesn't have a zero-argument constructor, especially in generic code. class Foo {} class Bar { typeof(scoped!Foo()) foo; this() { foo = scoped!Foo(); } }
Sep 26 2020
parent IGotD- <nise nise.com> writes:
On Saturday, 26 September 2020 at 11:30:23 UTC, k2aj wrote:
 It does work, the problem is that scoped returns a Voldemort 
 type, so you have to use 
 typeof(scoped!SomeClass(someConstructorArgs)) to declare a 
 field. Gets really annoying when doing it with any class that 
 doesn't have a zero-argument constructor, especially in generic 
 code.

 class Foo {}

 class Bar {
     typeof(scoped!Foo()) foo;
     this() {
         foo = scoped!Foo();
     }
 }
Thanks, so what we really need is a new scope template that declares the the variables in the class and possible a template for running the constructor?
Sep 27 2020