没有默认构造函数的C ++ / CLI值类型的基本原理 [英] Rationale for C++/CLI Value Types not having a default constructor

查看:64
本文介绍了没有默认构造函数的C ++ / CLI值类型的基本原理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于


价值等级X

{

public:

//不允许:X():i(100000),s(10000){}

//允许

void InitializeDefaults(){i = 100000; s = 10000; }

私人:

int i;

短s;

}


如何:


1)


X x;

x.InitializeDefaults();


语义比


2更好2)


X x;


为i设置默认值100000,为s设置10000?在

中,换句话说,为值类型删除自然用户定义的

默认构造函数并强制用户默认是什么原因

构造值类型对象为零或空值然后有

调用另一个函数来设置类型可能需要的默认值?

我读过2)不能保证正常发生但1)显然

可以吗?有人请向我解释一下两个语法动作的序列

如何比一个更好地保证正确实现。

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 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.

推荐答案

用于设置i的默认值100000和s的默认值为10000?换句话说,为值类型删除自然用户定义的默认构造函数并强制用户默认的基本原理是什么?将值类型对象构造为零或空值然后有没有调用另一个函数来设置类型可能需要的默认值?
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 ?




< from http://www.codecomments.com/archive292- 2006-2-806923.html>


如果您还查看C ++ - CLI Standard.pdf,请阅读:


12.2.1值类

值类是一个数据结构,包含字段,函数成员,

和嵌套类型。与其他类

类型不同,值类不支持用户定义的析构函数,终结符,

默认构造函数,复制

构造函数或复制赋值运营商。值类被设计为

允许CLI执行引擎

有效地复制值类对象。


-


亲切的问候,

Bruno van Dooren
br ********************** @ hotmail.com

仅删除_nos_pam



<from http://www.codecomments.com/archive292-2006-2-806923.html>

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.

--

Kind regards,
Bruno van Dooren
br**********************@hotmail.com
Remove only "_nos_pam"


Edward Diener写道:
Edward Diener wrote:
给定

值类X
{
public:
//不允许:X():i(100000),s(10000){}
//允许
void InitializeDefaults(){i = 100000; s = 10000; }
私人:
int i;
短片;
}

如何:

1)
X x;
x.InitializeDefaults();

比语言更好

2)

X x ;

为i设置默认值100000,为s设置10000?换句话说,为值类型删除自然的用户定义的默认构造函数并强制用户默认将值类型对象构造为零值或空值
的理由是什么? />然后必须调用另一个函数来设置类型可能需要的默认值?我读到2)不能保证发生正确但1)显然可以吗?有人请向我解释一下两个语法动作的序列如何比一个更好地保证正确实现。
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 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.




它'很简单:CLR希望能够默认构造一个值类型

0填充它的表示并且不能保证构造函数

将是叫。


-cd



It''s simple: The CLR expects to be able to default-construct a value type
by 0-filling it''s representation and cannot guarantee that a constructor
will be called.

-cd


这是一个奇怪的故事。 .NET的基本规范(Common Langauge

Infrastructure)确实支持无参数构造函数的值类型,

但只有半心半意。因此,大多数语言都不支持它们。

不幸的是,C ++托管扩展是一个例外。


不支持无参数ctros的最明显原因是快速

数组初始化。想想这个数组:


数组< V> ^ arrayOfVTs = gcnew aray< V>(1000000);


假设每个VT实例都是8个字节长。要初始化数组,使用类似于memset(数组数据的地址,800000000,0)的

指令。

这是可能的,因为VT不能有无参数的ctors 。使用

无参数ctor,这对于初始化数组是必要的:


for(int i = 0; i< 1000000; + i)

...为第i个元素调用VT .ctor ...


这将明显变慢


Marcus


" Edward Diener" < ED ******************* @ tropicsoft.com>在消息中写道

新闻:eC ************* @ TK2MSFTNGP03.phx.gbl ...
That''s a strange story. .NET''s base spec (the Common Langauge
Infrastructure) does support value types with parameterless constructors,
but only half hearted. Therefore, most languages do not support them.
Unfortunately, C++ managed extensions was an exception.

The most obvious reason why parameterless ctros are not supported is fast
array initialization. Think of this array:

array<V>^ arrayOfVTs = gcnew aray<V>(1000000);

Assume each VT instance is 8 bytes long. To initialize the array, an
instructroin similar to memset(address of array data, 800000000, 0) is used.
This is possible, because VTs can not have parameterless ctors. With a
parameterless ctor, this would be necessary to initialize the array:

for (int i = 0; i < 1000000; +i)
... call VT .ctor for i-th element ...

This would be significantly slower

Marcus

"Edward Diener" <ed*******************@tropicsoft.com> wrote in message
news:eC*************@TK2MSFTNGP03.phx.gbl...
给定
<值等级X
{
公开:
//不允许:X():i(100000),s(10000){}
//允许
void InitializeDefaults(){i = 100000; s = 10000; }
私人:
int i;
短片;
}

如何:

1)
X x;
x.InitializeDefaults();

比语言更好

2)

X x ;

为i设置默认值100000,为s设置10000?换句话说,为值类型删除自然用户定义的默认构造函数并强制用户默认将
值类型对象构造为零或空值的基本原理是什么必须调用另一个
函数来设置类型可能需要的默认值?我读到2)
不能保证正常发生但1)显然可以吗?有人
请向我解释一下两个语法动作的顺序如何比一个更好地保证正确实现。
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 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.



这篇关于没有默认构造函数的C ++ / CLI值类型的基本原理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆