Re: struct template help
Also, (*c).MYfunction() is leading to segmentation fault
Re: struct template help
sorry, I meant (*a).some_var not (*c).MYfunction()
Re: struct template help
On Saturday, 12 July 2014 at 19:16:52 UTC, Danyal Zia wrote: On Saturday, 12 July 2014 at 19:09:44 UTC, seany wrote: Please consider the following struct arc(T,U) { T some_var; U someother_var; } /* things */ class myclass { this(){} ~this(){} void MYfunction() { arc!(string, string[]) * a; a.some_var = hello; } } void main() { c = new myclass(); c.MYfunction(); } This leads to a segmentation fault. What am I doing wrong? a has not been instantiated. You are declaring it as a pointer to struct and using its fields without initializing it. arc!(string, string[]) a; will work. For reasons further down in the software, I need to do this with a pointer. How do I do it with a pointer, please?
Re: struct template help
On Saturday, 12 July 2014 at 19:19:28 UTC, seany wrote: For reasons further down in the software, I need to do this with a pointer. How do I do it with a pointer, please? I don't know what are you trying to achieve, but if that's what you want, you can do: void MYfunction() { auto strArr = [Hello, World!]; arc!(string, string[]) * a = new arc!(string, string[])(s, strArr); a.some_var = hello; } If you want to use a from outside the function you have to declare it in the class and then initialize it in your function.
Re: struct template help
do I have to initialize all variables of the struct? or may I also use a this(){} in the struct and initialize only those which are known at a given moment?
Re: struct template help
On 07/12/2014 12:19 PM, seany wrote: On Saturday, 12 July 2014 at 19:16:52 UTC, Danyal Zia wrote: On Saturday, 12 July 2014 at 19:09:44 UTC, seany wrote: arc!(string, string[]) * a; a.some_var = hello; a has not been instantiated. You are declaring it as a pointer to struct and using its fields without initializing it. arc!(string, string[]) a; will work. For reasons further down in the software, I need to do this with a pointer. How do I do it with a pointer, please? By making that pointer point to an actual object. :) arc!(string, string[]) * a = new arc!(string, string[])(); Shorter syntax: auto a = new arc!(string, string[]); You can assign it later as well: arc!(string, string[]) * a; // ... a = new arc!(string, string[])(); Preferably, don't assign to the member but construct when the information is available: auto a = new arc!(string, string[])(hello, [merhaba, hola, bonjour]); Ali
Re: struct template help
On 07/12/2014 12:32 PM, seany wrote: do I have to initialize all variables of the struct? No. The uninitialized ones get their .init values. or may I also use a this(){} in the struct and initialize only those which are known at a given moment? That already works with structs. You don't need to define any constructor if the type is simple data i.e. if you don't want to set any invariants, just construct with whatever is available at that moment: auto a = new S(hello); However, if it is important that someother_var should not remain as an empty array, then you better define a constructor and demand both values when constructing. Ali
Re: struct template help
On Saturday, 12 July 2014 at 19:32:48 UTC, seany wrote: do I have to initialize all variables of the struct? or may I also use a this(){} in the struct and initialize only those which are known at a given moment? You can initialize in constructor this(), but you can't initialize partial fields of struct when using pointer to struct. There would be other trivial ways to do what you are trying to do. Also, I never used any pointer to struct (which is of value type) in my code .
Re: struct template help
On 07/12/2014 12:38 PM, Danyal Zia wrote: You can initialize in constructor this(), but you can't initialize partial fields of struct when using pointer to struct. Actually, that works too but members must be initialized from the beginning. The trailing ones are left with .init values: struct S { int i; string s; } void main() { auto s = new S(42); static assert(is (typeof(s) == S*)); } Ali
Re: struct template help
On Saturday, 12 July 2014 at 19:42:13 UTC, Ali Çehreli wrote: Actually, that works too but members must be initialized from the beginning. The trailing ones are left with .init values: struct S { int i; string s; } void main() { auto s = new S(42); static assert(is (typeof(s) == S*)); } Ali Ah, right. I still has C++ background in my veins I guess :)