472,958 Members | 2,191 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

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 5729
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: news.microsoft.com | last post by:
Hi, I am using structs and am also using property accessors to access those private member fields... TO me this is a good way of handling them, but I find alot of people using direct access to...
4
by: Peter Rilling | last post by:
Hi. Two of my biggest complaints about structure in c# is that 1) You cannot implement a default constructor and 2) You cannot initialize fields on the declaration line. I would like to...
2
by: mario.demiguel | last post by:
Does the standard support structs have constructors?
12
by: Edward Diener | last post by:
Given value class X { public: // Not allowed: X():i(100000),s(10000) { } // Allowed void InitializeDefaults() { i = 100000; s = 10000; } private: int i;
12
by: Ole Nielsby | last post by:
Why is this? I've stumbled on this restriction more than once, and I'd like to know the philosophy behind it if there is one. I figure I'd be less prone to make this error if I knew the reason....
7
by: Markus Svilans | last post by:
Hi, What is the difference between having a struct with constructors and methods versus a class? In many C++ examples online I have seen code similar to this: struct Animal { Animal()
23
by: Jess | last post by:
Hello, I understand the default-initialization happens if we don't initialize an object explicitly. I think for an object of a class type, the value is determined by the constructor, and for...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
43
by: JohnQ | last post by:
Are a default constructor, destructor, copy constructor and assignment operator generated by the compiler for a struct if they are not explicitely defined? I think the answer is yes, because...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.