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==] 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==]
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==]
> 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==]
"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
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
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
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
"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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
by: mario.demiguel |
last post by:
Does the standard support structs have constructors?
|
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;
|
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....
|
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()
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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 :...
|
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...
|
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...
|
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...
|
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...
|
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...
| |