digitalmars.D.learn - parameter-less "default" struct constructor not allowed--why?
- Christopher the Magnificent (13/13) May 20 2011 Why are parameter-less struct constructors not allowed?
- Andrej Mitrovic (2/2) May 20 2011 You can override opCall(). I don't know how people feel about using
- Christopher the Magnificent (15/17) May 20 2011 If I override opCall(), will I be able to use that will operator "new"
- Andrej Mitrovic (1/1) May 20 2011 http://www.digitalmars.com/d/archives/digitalmars/D/Why_no_struct_defaul...
- Jonathan M Davis (3/5) May 20 2011 LOL. You just saved me a lot of typing. I was basically going to write o...
- Andrej Mitrovic (4/9) May 20 2011 You should keep a hash of all your posts which you can quickly look up
Why are parameter-less struct constructors not allowed? I have a struct that doesn't need vtable or inheritance or anything, but I want to automatically call a method on it when it's created. I also want to create this struct on the heap using operator "new". Is there any way I can keep this struct as a struct (rather than a class) and still be able to allocate it and initialize it (including execution of my setup code) in a single statement, a la the following? MyStruct* ms = new MyStruct(); Maybe I'll have to introduce a dummy parameter in order to be include a struct constructor. You think so? What is the rationale to D's not permitting parameter-less "default" struct constructors?
May 20 2011
You can override opCall(). I don't know how people feel about using that sort of thing, but it works for me (I rarely need it though).
May 20 2011
On 5/20/11 4:25 PM, Andrej Mitrovic wrote:You can override opCall(). I don't know how people feel about using that sort of thing, but it works for me (I rarely need it though).If I override opCall(), will I be able to use that will operator "new" to do BOTH of the following? 1. MyStruct ms1 = MyStruct(); // calls opCall to initialize on stack 2. MyStruct* ms2 = new MyStruct(); // calls opCall() to init on heap What this suggests to me is that opCall override and using keyword "new" do not mix. Would you agree? This leads me back to my original question. *Why* are zero-parameter struct constructors disallowed? Any insight or speculation? If there are no valid reasons for this limitation, then perhaps its ought to be lifted. What do you think, Mr. Mitrovic, or anyone? -- Christopher
May 20 2011
http://www.digitalmars.com/d/archives/digitalmars/D/Why_no_struct_default_constructors_129559.html
May 20 2011
http://www.digitalmars.com/d/archives/digitalmars/D/Why_no_struct_default_c onstructors_129559.htmlLOL. You just saved me a lot of typing. I was basically going to write out that post again... - Jonathan M Davis
May 20 2011
On 5/21/11, Jonathan M Davis <jmdavisProg gmx.com> wrote:You should keep a hash of all your posts which you can quickly look up by key, retrieve the link and then just paste that as your response. You could even do it in D, it has hashes! :phttp://www.digitalmars.com/d/archives/digitalmars/D/Why_no_struct_default_c onstructors_129559.htmlLOL. You just saved me a lot of typing. I was basically going to write out that post again... - Jonathan M Davis
May 20 2011