470,833 Members | 1,337 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,833 developers. It's quick & easy.

value structs : can they have default constructors?

I've been told that value structs can have default constructors (I'm using
VS C++.NET 2005 PRO using clr:/pure syntax). But the following code
generates the following error:

value struct ValueStruct
{
double x ;
double y ;
ValueStruct() { x = y = double(0) ; } // error *
} ;

* error C3417: ValueStruct::ValueStruct(void)' : value types cannot contain
user-defined special member functions

Was I misinformed, or am I doing something wrong in the above code?

[==P==]
Feb 14 '06 #1
9 5571
If you look in "CSharpFinalWorkingDraftApril2005.pdf", you read :

Because every value type implicitly has a public parameterless instance
constructor, it is not possible for a
struct type to contain an explicit declaration of a parameterless
constructor. A struct type is however
permitted to declare parameterized instance constructors (§18.3.8).

jmd

"Peter Oliphant" <po*******@RoundTripInc.com> wrote in message
news:Ol**************@TK2MSFTNGP09.phx.gbl...
I've been told that value structs can have default constructors (I'm using
VS C++.NET 2005 PRO using clr:/pure syntax). But the following code
generates the following error:

value struct ValueStruct
{
double x ;
double y ;
ValueStruct() { x = y = double(0) ; } // error *
} ;

* error C3417: ValueStruct::ValueStruct(void)' : value types cannot
contain user-defined special member functions

Was I misinformed, or am I doing something wrong in the above code?

[==P==]

Feb 14 '06 #2
If you also look in "C++-CLI Standard.pdf", you read :

12.2.1 Value classes
A value class is a data structure that contains fields, function members, and nested types. Unlike other class
types, value classes do not support user-defined destructors, finalizers, default constructors, copy
constructors, or copy assignment operators. Value classes are designed to allow the CLI execution engine to
efficiently copy value class objects.

Hope that helps.
jmd

"Peter Oliphant" <po*******@RoundTripInc.com> wrote in message news:Ol**************@TK2MSFTNGP09.phx.gbl...
I've been told that value structs can have default constructors (I'm using
VS C++.NET 2005 PRO using clr:/pure syntax). But the following code
generates the following error:

value struct ValueStruct
{
double x ;
double y ;
ValueStruct() { x = y = double(0) ; } // error *
} ;

* error C3417: ValueStruct::ValueStruct(void)' : value types cannot contain
user-defined special member functions

Was I misinformed, or am I doing something wrong in the above code?

[==P==]

Feb 14 '06 #3
> Because every value type implicitly has a public parameterless instance
constructor, it is not possible for a
struct type to contain an explicit declaration of a parameterless
constructor. A struct type is however
permitted to declare parameterized instance constructors (§18.3.8).
How does the implicit parameterless (i.e., default) constructor determine
the values it gives it members?

Anyway, thanks! This clarifies it for me. I got the exact opposite advice,
or more likely, I misinterpreted the advice (previously) given to me.

So, as I NOW understand it, I can create any number of constructors for a
value struct EXCEPT the default one. That is, I can create constructors
which HAVE parameters. I just tested this, and it does work... : )

[==P==]

"jmd.msdn" <jm******@newsgroup.nospam> wrote in message
news:uZ**************@TK2MSFTNGP11.phx.gbl... If you look in "CSharpFinalWorkingDraftApril2005.pdf", you read :

Because every value type implicitly has a public parameterless instance
constructor, it is not possible for a
struct type to contain an explicit declaration of a parameterless
constructor. A struct type is however
permitted to declare parameterized instance constructors (§18.3.8).

jmd

"Peter Oliphant" <po*******@RoundTripInc.com> wrote in message
news:Ol**************@TK2MSFTNGP09.phx.gbl...
I've been told that value structs can have default constructors (I'm
using VS C++.NET 2005 PRO using clr:/pure syntax). But the following code
generates the following error:

value struct ValueStruct
{
double x ;
double y ;
ValueStruct() { x = y = double(0) ; } // error *
} ;

* error C3417: ValueStruct::ValueStruct(void)' : value types cannot
contain user-defined special member functions

Was I misinformed, or am I doing something wrong in the above code?

[==P==]


Feb 14 '06 #4
"Peter Oliphant" <po*******@RoundTripInc.com> wrote
So, as I NOW understand it, I can create any number of constructors for a
value struct EXCEPT the default one. That is, I can create constructors
which HAVE parameters. I just tested this, and it does work... : )


Value types can't have special member functions. You therefore
also cannot provide a copy constructor for a value type.

-hg
Feb 15 '06 #5
I guess I then need the definition of 'special member functions'.

Based on what you've said, a copy constructor is a special member function.
But a constructor which, I guess, DOESN'T have as its only parameter a
pointer or reference to the same class type (i.e., isn't a copy constructor)
IS allowed. Why the destinction? Or, like I said, and more importantly what
defines a 'special member function' in contrast to one that isn't 'special'?

[==P==]

"Holger Grund" <ho**********@remove.ix-n.net> wrote in message
news:OC**************@TK2MSFTNGP11.phx.gbl...
"Peter Oliphant" <po*******@RoundTripInc.com> wrote
So, as I NOW understand it, I can create any number of constructors for a
value struct EXCEPT the default one. That is, I can create constructors
which HAVE parameters. I just tested this, and it does work... : )


Value types can't have special member functions. You therefore
also cannot provide a copy constructor for a value type.

-hg

Feb 15 '06 #6
Look at http://www.plumhall.com/ecma/index.html
and download the 1.15 C++/CLI draft.

"Peter Oliphant" <po*******@RoundTripInc.com> wrote in message
news:uF**************@TK2MSFTNGP12.phx.gbl...
I guess I then need the definition of 'special member functions'.

Based on what you've said, a copy constructor is a special member
function. But a constructor which, I guess, DOESN'T have as its only
parameter a pointer or reference to the same class type (i.e., isn't a
copy constructor) IS allowed. Why the destinction? Or, like I said, and
more importantly what defines a 'special member function' in contrast to
one that isn't 'special'?

[==P==]

"Holger Grund" <ho**********@remove.ix-n.net> wrote in message
news:OC**************@TK2MSFTNGP11.phx.gbl...
"Peter Oliphant" <po*******@RoundTripInc.com> wrote
So, as I NOW understand it, I can create any number of constructors for
a value struct EXCEPT the default one. That is, I can create
constructors which HAVE parameters. I just tested this, and it does
work... : )


Value types can't have special member functions. You therefore
also cannot provide a copy constructor for a value type.

-hg


Feb 15 '06 #7
Peter Oliphant wrote:
How does the implicit parameterless (i.e., default) constructor determine
the values it gives it members?


By memset'ing (filling) the entire class with all 0 bytes, regardless of
the actual type of the member variables. Think of value types as PODs
(plain old data, like a C struct). When a value type is copied, the CLR
is simply doing a memcpy on the underlying byte-content (as a raw
octet-stream). If this doesn't satisfy your goals, you must use a ref class.

Tom
Feb 16 '06 #8
"Peter Oliphant" <po*******@RoundTripInc.com> wrote
I guess I then need the definition of 'special member functions'.
Special member functions are defined by the C++ standard.
These are:
- default constructor
- copy constructor
- copy assignment operator
- destructor
Based on what you've said, a copy constructor is a special member
function. But a constructor which, I guess, DOESN'T have as its only
parameter a pointer or reference to the same class type (i.e., isn't a
copy constructor) IS allowed. Why the destinction? Or, like I said, and
more importantly what defines a 'special member function' in contrast to
one that isn't 'special'?

The CLR doesn't have concept of copy construction during in ordinary
control flow.

Therefore, there's no way for the C++ compiler to emit information
that other language implementations will understand to implement the
C++ semantics. The language designers apparently figured that this
would cause confusion (and FWIW I disagree) and hence decided
not to allow special member functions. Only special member functions
are called implicitly by the compiler-generated code.

For instance,
// C++
value struct X {
X(){ /*..*/ }
X( const X%){/*..*/}
X% operator=(const X%){ /*...*/};
}

// C# client
void foo() {
X x; // doesn't invoke default ctor
new X[] ( 10 ); // doesn't invoke default ctor for elements
X y = x; // doesn't invoke copy ctor
y = x; // doesn't invoke copy assignment operator
} // doesn't invoke destructor for x or y

-hg
Feb 16 '06 #9
This is good. That's exactly how I personally think of as the 'natural' way
to default-fill data types one doesn't know anything about. So in this I am
happy! : )

[==P==]

"Tamas Demjen" <td*****@yahoo.com> wrote in message
news:uj**************@TK2MSFTNGP12.phx.gbl...
Peter Oliphant wrote:
How does the implicit parameterless (i.e., default) constructor determine
the values it gives it members?


By memset'ing (filling) the entire class with all 0 bytes, regardless of
the actual type of the member variables. Think of value types as PODs
(plain old data, like a C struct). When a value type is copied, the CLR is
simply doing a memcpy on the underlying byte-content (as a raw
octet-stream). If this doesn't satisfy your goals, you must use a ref
class.

Tom

Feb 17 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by news.microsoft.com | last post: by
4 posts views Thread by Peter Rilling | last post: by
2 posts views Thread by mario.demiguel | last post: by
7 posts views Thread by Markus Svilans | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.