Re: remove struct's default constructor?
- From: "Bruce Wood" <brucewood@xxxxxxxxxx>
- Date: 8 Mar 2007 17:34:36 -0800
On Mar 8, 5:03 pm, "Carl Daniel [VC++ MVP]"
<cpdaniel_remove_this_and_nos...@xxxxxxxxxxxxxxx> wrote:
"Zytan" <zytanlith...@xxxxxxxxx> wrote in message
news:1173401460.163333.165530@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Yes, junk would exist if no constructor is called. But, if I force
any use of a struct to use my constructor, then the values will be
what the constructor forces them to be. This is even better than
having them zeroed out. Imagine only having proper values. Zerod
values are better than junk values. Proper values are even better
than zeroed values.
It comes down to this: There's a deeply rooted assumption in the CLR that:
1. value types can always be copied as memory images.
2. that storage for a value type can always be initialized by filling the
memory with zeros.
Ah. Finally. I was going to point that out.
Willy is right: in effect there _is no default constructor_. It was
omitted for value types for efficiency reasons. When you say:
public class Foo
{
private int x;
private float y;
private MyStruct z;
}
what happens is that all of the private field space for Foo is filled
with zeroes. The three fields aren't constructed individually. Then
Foo's constructor is run. If Foo's constructor forgets to initialize z
is some meaningful way, then it is left as it was: filled with zeroes.
All references it may contain are null, all integers are zero, all
booleans are false.
There is no default constructor, neither implied nor explicit. That is
to say, there is no piece of code emitted with the express job of
initializing MyStruct z, just some code that runs to generally
initialize the memory where Foo's state will go. This was, I believe
to make it cheap to create value types: there is never a need to call
implicit constructors: just a need to call explicit constructors if
you should invoke them in your code.
So what do you do in your MyStruct to handle this? One of two things:
1) Have MyStruct explicitly check for an un-constructed state and flag
it as illegal, usually by throwing an exception on property access /
method calls that can't be done before the struct is constructed
explicitly. I tend to avoid this because it just causes a mess at run-
time.
2) Give all-zeroes an interpretation. Decide what it means, and handle
that situtation. For example, in my Measure struct, all zeroes (and
nulls) means a scalar zero measurement (zero with no unit of measure
attached).
Structs are supposed to be very small and very simple. If your struct
starts to grow to be large and/or complicated then IMHO it's time to
re-evaluate the design.
.
- Follow-Ups:
- Re: remove struct's default constructor?
- From: Zytan
- Re: remove struct's default constructor?
- References:
- remove struct's default constructor?
- From: Zytan
- Re: remove struct's default constructor?
- From: Willy Denoyette [MVP]
- Re: remove struct's default constructor?
- From: Zytan
- Re: remove struct's default constructor?
- From: Willy Denoyette [MVP]
- Re: remove struct's default constructor?
- From: Zytan
- Re: remove struct's default constructor?
- From: Carl Daniel [VC++ MVP]
- remove struct's default constructor?
- Prev by Date: help debugging DataGridViewComboBoxColumn not setting value
- Next by Date: Re: remove struct's default constructor?
- Previous by thread: Re: remove struct's default constructor?
- Next by thread: Re: remove struct's default constructor?
- Index(es):
Relevant Pages
|