digitalmars.D.learn - struct template help
- seany (24/24) Jul 12 2014 Please consider the following
- seany (1/1) Jul 12 2014 Also, (*c).MYfunction() is leading to segmentation fault
- seany (2/2) Jul 12 2014 sorry, I meant (*a).some_var
- Danyal Zia (4/28) Jul 12 2014 "a" has not been instantiated. You are declaring it as a pointer
- seany (3/41) Jul 12 2014 For reasons further down in the software, I need to do this with
- Danyal Zia (12/14) Jul 12 2014 I don't know what are you trying to achieve, but if that's what
- seany (3/3) Jul 12 2014 do I have to initialize all variables of the struct? or may I
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (10/14) Jul 12 2014 That already works with structs. You don't need to define any
- Danyal Zia (6/9) Jul 12 2014 You can initialize in constructor this(), but you can't
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (14/16) Jul 12 2014 Actually, that works too but members must be initialized from the
- Danyal Zia (2/15) Jul 12 2014 Ah, right. I still has C++ background in my veins I guess :)
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (14/23) Jul 12 2014 By making that pointer point to an actual object. :)
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?
Jul 12 2014
Also, (*c).MYfunction() is leading to segmentation fault
Jul 12 2014
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.
Jul 12 2014
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:For reasons further down in the software, I need to do this with a pointer. How do I do it with a pointer, please?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.
Jul 12 2014
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.
Jul 12 2014
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?
Jul 12 2014
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
Jul 12 2014
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 .
Jul 12 2014
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
Jul 12 2014
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*)); } AliAh, right. I still has C++ background in my veins I guess :)
Jul 12 2014
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";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"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?
Jul 12 2014