Given
value class X
{
public:
// Not allowed: X():i(100000),s(10000) { }
// Allowed
void InitializeDefaults() { i = 100000; s = 10000; }
private:
int i;
short s;
}
How can:
1)
X x;
x.InitializeDefaults();
be better semantically than
2)
X x;
for setting the default values of 100000 for i and 10000 for s In other words what is the rationale for removing the natural user-defined default constructor for value types and forcing the user to default construct the value type object to its zero or null values and then have to call another function to set default values which the type may want I read that 2) can not be guaranteed to occur properly in the face of a default constructor but 1) evidently can. Someone please explain to me how a sequence of two syntax actions has a better guarantee of properly being implemented than just one, since both are essentially doing the exact same thing and 1) is obviously more verbose than 2)..

Rationale for C++/CLI Value Types not having a default constructor
Alkiris
value class X
{
public:
X() { InializeDefaults(); }
void InitializeDefaults() { i = 100000; s = 10000; }
private:
int i;
short s;
}
KerryDBerry
I never heard that one was better than the other or even that one could not occur correctly (this would be very bad if it happened).
One reason for moving the initializations from the constructor to an init function would be if during the life time of your object you'd need to reset it to its initial values, in that case it makes sense that you have an init function so you can call it to initialize your object again.
xiaopang
The canonical example is an array of value types - the CLR will not call the default constructor to initialize each element of the array.
So Instead of leaving the C++ user in a position where the default constructor might be called in some cases and not in others - not to mention the possibility that what these cases are may change from one release of the CLR to another - the Visual C++ team decided to come down on the side of consistancy and disallow default constructors on value-types.
Note: value-types are zero initialized so every value-type needs to be able to accept the zeroed-state as a valid instance.
dalmond
Anyway what's the point. To construct value types with a user-defined set of starting values I will have to call some kludgy function after default construction to do it. Horrendous but since it doesn't bother MS why should it bother me..
Kary
Perro
In other words, the ability to default construct a value class object to something other than 0 s must be a total kludge. This despite the fact that all the OO languages of which I know support user-defined default constructors for all "classes".
MikeCad
ch_cu
Performance.
Value-types are not and were not designed to be OO types - they are sealed so you cannot inherit from them, virtual functions (if they have any) are called non-virtually. Value-types are design for performance - they should be used to represent discrete types (like a point). If you want full OO types then you should use a reference-type.