Re: Structs cannot contain explicit parameterless constructors
- From: "Ole Nielsby" <ole.nielsby@xxxxxxxxxxxx>
- Date: Sat, 6 May 2006 02:29:42 +0200
Nick Hounsome <News@xxxxxxxxxxxxxxxxxx> wrote:
I don't know what the real reason is but it may have something
to do with version compatibility - Normally you can replace a
dll with a new version using the same method signatures and
everything will work OK but this would not be possible with
default constructors because the compiler does not actually
call a default constructor - it just makes the memory 0 for
efficiency reasons - consequently, If you added a default
constructor it would not be called unless you recompiled.
Makes some sort of sense, though I'm not at ease with
this kind of deep-tech implementation detail having too
much influence on the language design.
Regarding the original post - I think that the design is
misguided
Original poster disagrees, but this might be a matter of
slightly different programming styles, yours perhaps
being tuned by experience with C# to stay away from
its weak spots, while I - as a functional programming
freak involved with language desing - have trained
myself to question the language.
- If you need a unique identity in a class then
assign it in the class (or better a base class)
You can use an IdentityStamp class to get it if
you want using a static method [eg this.id =
IdentityStamp.GetNext();]
That's what I did, to get things runnig, not because I
deemed it reasonable.
Assuming that you could do what the OP wanted
it would not behave as expected:
// IdentityStamp.seed == 0
Obj obj = new Obj(); // class usng IdentityStamp
IdentityStamp id = obj.Id;
Foo(id); // void Foo(IdentityStamp id)
// IdentityStamp.seed is now at least 3.
This would create a new stamp then overwrite it
with the one from obj which is probably not what
you intended.
C++ gets around this with copy constructors (even
the line that looks like assignment!). In C# there is
no copy constructor to fix the problem for you.
This still seems a bit obscure.
Are you implying that the line
IdentityStamp id = obj.Id;
is treated as being equivalent of
IdentityStamp id = new IdentityStamp();
id = obj.Id;
and
Foo(id)
is treated as something like:
Foo(new IdentityStamp() = id)
???
Considering how C# handles classes, I find these
implicit constructor calls quite unnecessary - why
can't the compiler just zero the struct and flag it as
uninitialized, just as a class ref would be nulled and
flagged uninitialized?
Is this because the clr uses default constructors to
ensure floats are properly zeroed or something like
that?
Regards/Ole N.
.
- Follow-Ups:
- Re: Structs cannot contain explicit parameterless constructors
- From: Nick Hounsome
- Re: Structs cannot contain explicit parameterless constructors
- References:
- Structs cannot contain explicit parameterless constructors
- From: Ole Nielsby
- Re: Structs cannot contain explicit parameterless constructors
- From: Mythran
- Re: Structs cannot contain explicit parameterless constructors
- From: Nick Hounsome
- Structs cannot contain explicit parameterless constructors
- Prev by Date: How to remove xmlns attribute from XML document (.net)
- Next by Date: Dynamic cast possible?
- Previous by thread: Re: Structs cannot contain explicit parameterless constructors
- Next by thread: Re: Structs cannot contain explicit parameterless constructors
- Index(es):
Relevant Pages
|