删除struct的默认构造函数? [英] remove struct's default constructor?

查看:116
本文介绍了删除struct的默认构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个结构构造函数来初始化我的所有私有(或公共

readonly)字段。仍然存在将

全部设置为零的默认构造函数。有没有办法删除这个

隐式默认构造函数的创建,只通过我的

构造函数强制创建一个struct?


Zytan

解决方案

2007年3月8日14:21:39 -0800,Zytan < zy ********** @ yahoo.comwrote:


>我有一个结构构造函数来初始化我的所有私人(或公共)
readonly)字段。仍然存在将它们全部设置为零的默认构造函数。有没有办法删除这个
隐式默认构造函数的创建,只通过我的
构造函数强制创建一个struct?

Zytan


声明你自己的私有默认构造函数:

struct MyStruct {


public MyStruct(int data){

//做东西

}


私人MyStruct(){}

}


因为你声明了自己的默认构造函数,所以编译器

不会创建一个。因为你已经宣布私人没有人

,除非你可以打电话给它。这使你的结构无法使用
默认构造,这将限制它在某些情况下的使用。


rossum





" rossum" < ro ****** @ coldmail.com写信息

新闻:oc ************************ ******** @ 4ax.com ...


2007年3月8日14:21:39 -0800,Zytan < zy ********** @ yahoo.comwrote:


>>我有一个结构构造函数来初始化我的所有私有(或公共
readonly)字段。仍然存在将它们全部设置为零的默认构造函数。有没有办法删除这个
隐式默认构造函数的创建,只通过我的
构造函数强制创建一个struct?

Zytan


声明你自己的私有默认构造函数:

struct MyStruct {


public MyStruct(int data){

//做东西

}


私人MyStruct(){}

}



错误CS0568:结构不能包含显式无参数构造函数


David


" Zytan" < zy ********** @ yahoo.comwrote in message

news:11 ******************** **@8g2000cwh.googlegrou ps.com ...


>我有一个结构构造函数来初始化我的所有私人(或公共
$ b) $ b readonly)字段。仍然存在将

全部设置为零的默认构造函数。有没有办法删除这个

隐式默认构造函数的创建,只通过我的

构造函数强制创建一个struct?


Zytan



不确定你的默认构造函数是什么意思,没有默认这样的东西

结构的构造函数。

如果你不提供构造函数,结构将由运行时初始化,这样它的所有字段都有它们的默认值,即是的,结构中的内存空间(堆栈上的fi)由结构设置为0.没有办法抑制这个初始化,这是一个

好东西。


威利。


I have a struct constructor to initialize all of my private (or public
readonly) fields. There still exists the default constructor that sets
them all to zero. Is there a way to remove the creation of this
implicit default constructor, to force the creation of a struct via my
constructor only?

Zytan

解决方案

On 8 Mar 2007 14:21:39 -0800, "Zytan" <zy**********@yahoo.comwrote:

>I have a struct constructor to initialize all of my private (or public
readonly) fields. There still exists the default constructor that sets
them all to zero. Is there a way to remove the creation of this
implicit default constructor, to force the creation of a struct via my
constructor only?

Zytan

Declare your own default constructor private:

struct MyStruct {

public MyStruct(int data) {
// Do stuff
}

private MyStruct() { }
}

Because you have declared your own default constructor the compiler
will not create one. Because you have declared it private no-one
except you can ever call it. This makes your struct impossible to
default construct, which will limit its use in some situations.

rossum




"rossum" <ro******@coldmail.comwrote in message
news:oc********************************@4ax.com...

On 8 Mar 2007 14:21:39 -0800, "Zytan" <zy**********@yahoo.comwrote:

>>I have a struct constructor to initialize all of my private (or public
readonly) fields. There still exists the default constructor that sets
them all to zero. Is there a way to remove the creation of this
implicit default constructor, to force the creation of a struct via my
constructor only?

Zytan

Declare your own default constructor private:

struct MyStruct {

public MyStruct(int data) {
// Do stuff
}

private MyStruct() { }
}

error CS0568: Structs cannot contain explicit parameterless constructors

David


"Zytan" <zy**********@yahoo.comwrote in message
news:11**********************@8g2000cwh.googlegrou ps.com...

>I have a struct constructor to initialize all of my private (or public
readonly) fields. There still exists the default constructor that sets
them all to zero. Is there a way to remove the creation of this
implicit default constructor, to force the creation of a struct via my
constructor only?

Zytan


Not sure what you mean here with default constructor, there is no such thing as a "default"
constructor for structs.
If you don''t supply a constructor, the struct will get initialized by the runtime, such that
all its fields have their default value, that is, the memory space (f.i on the stack) taken
by the struct is set to 0. There is no way to suppress this "initialization ", which is a
good thing.

Willy.


这篇关于删除struct的默认构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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