| Thread overview | ||||||||
|---|---|---|---|---|---|---|---|---|
|
May 20, 2011 parameter-less "default" struct constructor not allowed--why? | ||||
|---|---|---|---|---|
| ||||
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 Re: parameter-less "default" struct constructor not allowed--why? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Christopher the Magnificent | 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 Re: parameter-less "default" struct constructor not allowed--why? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | 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
It seems that #2 does not work on my machine.
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 Re: parameter-less "default" struct constructor not allowed--why? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Christopher the Magnificent | http://www.digitalmars.com/d/archives/digitalmars/D/Why_no_struct_default_constructors_129559.html | |||
May 21, 2011 Re: parameter-less "default" struct constructor not allowed--why? | ||||
|---|---|---|---|---|
| ||||
> http://www.digitalmars.com/d/archives/digitalmars/D/Why_no_struct_default_c onstructors_129559.html
LOL. You just saved me a lot of typing. I was basically going to write out that post again...
- Jonathan M Davis
| ||||
May 21, 2011 Re: parameter-less "default" struct constructor not allowed--why? | ||||
|---|---|---|---|---|
| ||||
On 5/21/11, Jonathan M Davis <jmdavisProg@gmx.com> wrote:
>> http://www.digitalmars.com/d/archives/digitalmars/D/Why_no_struct_default_c onstructors_129559.html
>
> LOL. You just saved me a lot of typing. I was basically going to write out that post again...
>
> - Jonathan M Davis
>
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! :p
| ||||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply