复制构造函数和克隆 [英] Copy constructors and clones

查看:67
本文介绍了复制构造函数和克隆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自C ++世界我无法理解为什么在.NET框架中不使用复制

构造函数。复制构造函数从另一个同类对象的副本创建一个对象
。听起来很简单

但显然.NET出于某种原因难以理解这个概念。我知道

明白.NET对象是在GC堆上创建的但是没有
意味着他们不能从同一个对象的另一个对象复制当他们被创建时,他们很友好。


我知道.NET有一个ICloneable接口,用于制作一个深度为
的b / b一个对象的副本,它甚至有一个受保护的MemberWiseClone,可以在内部用于浅拷贝。但令我惊讶的是,即使是
ICloneable.Clone()有时也不用于制作对象的深层副本

但是使用了一些其他特定的类成员函数。


作为一个例子,当我查看system.string类时,我发现它没有
有一个复制构造函数,无论出于什么原因我都没有理解似乎

正常的.NET。然后我查看Clone()函数,期望它生成字符串'的内容的

副本并将其作为新字符串返回给我。不,它不是b $ b也不是那样做的。最后我找到了执行

工作的Copy()函数。所以不要从旧的字符串创建一个新的字符串,我必须做

奥术(对我而言)''字符串x = string.Copy(old_instance);''而不是

simple''string x = new string(old_instance)''。


评论和解释?

Coming from the C++ world I can not understand the reason why copy
constructors are not used in the .NET framework. A copy constructor creates
an object from a copy of another object of the same kind. It sounds simple
but evidently .NET has difficulty with this concept for some reason. I do
understand that .NET objects are created on the GC heap but that doesn''t
mean that they couldn''t be copied from another object of the same kind when
they are created.

I understand that .NET has an ICloneable interface which is used to make a
deep copy of an object, and that it even has a protected MemberWiseClone to
be used internally for shallow copies. But much to my surprise even
ICloneable.Clone() is sometimes not used to make a deep copy of an object
but some other particular class member function is used instead.

As an example when I look at the system.string class I find that it does not
have a copy constructor, which for whatever reason I do not understand seems
normal for .NET. Then I look at the Clone() function, expecting it to make a
copy of the string''s contents and return it to me as a new string. No, it
doesn''t do that either. Finally I find the Copy() function which does the
job. So instead of creating a new string from an old one, I must do the
arcane ( for me ) ''string x = string.Copy(old_instance);'' rather than a
simple ''string x = new string(old_instance)''.

Comments and explanations ?

推荐答案

嗨爱德华


VB.net

dim newstring as string = oldstring

C#

string newstring = oldstring;

C ++如你所说

string newstring = new string(oldstring)


我想你喜欢打字吗?


评论:


永远不要尝试使用你用过的语言旧的陈述。


例如,一旦有人告诉我用他的语言他只使用了50个代码来进行

a排序,他将其翻译成我当时使用的那个和他的200行

代码


他没有看到有一个政治家排序从一行。

(它也快得多)


Cor

Hi Edward

VB.net
dim newstring as string = oldstring
C#
string newstring = oldstring;
C++ as you said
string newstring = new string(oldstring)

I think you love to type?

Comments:

Never try to use your used old statements from you used language.

Example, once someone told me that in his language he only used 50 code for
a sort, he had translated it to the one I was using then and he 200 lines of
code

He had not seen that there was a statemen "sort" from one line.
(It was also much faster)

Cor

来自C ++世界我无法理解为什么在.NET框架中不使用复制
构造函数。复制构造函数
从同一类型的另一个对象的副本创建一个对象。听起来很简单
但显然.NET出于某种原因难以理解这个概念。我知道.NET对象是在GC堆上创建的,但这并不意味着它们不能从同一类型的另一个对象中复制出来。创建。

我知道.NET有一个ICloneable接口,用于制作一个对象的深层副本,它甚至还有一个受保护的MemberWiseClone
来使用内部为浅拷贝。但令我惊讶的是,即使ICloneable.Clone()有时也不用于制作对象的深层副本,而是使用其他一些特定的类成员函数。

作为一个例子,当我查看system.string类时,我发现它确实
没有复制构造函数,无论出于什么原因我不理解
似乎是正常的.NET。然后我查看Clone()函数,期望它将
作为字符串'的内容的副本并将其作为新字符串返回给我。不,它也不会这样做。最后我找到了执行
工作的Copy()函数。因此,不是从旧的字符串创建一个新的字符串,我必须做的是
奥术(对我而言)''字符串x = string.Copy(old_instance);''而不是简单的''字符串x =新字符串(old_instance)''。

评论和解释?
Coming from the C++ world I can not understand the reason why copy
constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same kind. It sounds simple
but evidently .NET has difficulty with this concept for some reason. I do
understand that .NET objects are created on the GC heap but that doesn''t
mean that they couldn''t be copied from another object of the same kind when they are created.

I understand that .NET has an ICloneable interface which is used to make a
deep copy of an object, and that it even has a protected MemberWiseClone to be used internally for shallow copies. But much to my surprise even
ICloneable.Clone() is sometimes not used to make a deep copy of an object
but some other particular class member function is used instead.

As an example when I look at the system.string class I find that it does not have a copy constructor, which for whatever reason I do not understand seems normal for .NET. Then I look at the Clone() function, expecting it to make a copy of the string''s contents and return it to me as a new string. No, it
doesn''t do that either. Finally I find the Copy() function which does the
job. So instead of creating a new string from an old one, I must do the
arcane ( for me ) ''string x = string.Copy(old_instance);'' rather than a
simple ''string x = new string(old_instance)''.

Comments and explanations ?



>作为一个例子,当我查看system.string类时,我发现它确实没有。
>没有
> As an example when I look at the system.string class I find that it does
not
有一个复制构造函数,无论出于什么原因我都不理解
对于.NET来说似乎很正常。然后我查看Clone()函数,期望它将
作为字符串'的内容的副本并将其作为新字符串返回给我。不,它也不会这样做。最后我找到了执行
工作的Copy()函数。因此,不是从旧的字符串创建一个新的字符串,我必须做的是
奥术(对我而言)''字符串x = string.Copy(old_instance);''而不是简单的''字符串x = new string(old_instance)''。
have a copy constructor, which for whatever reason I do not understand seems normal for .NET. Then I look at the Clone() function, expecting it to make a copy of the string''s contents and return it to me as a new string. No, it
doesn''t do that either. Finally I find the Copy() function which does the
job. So instead of creating a new string from an old one, I must do the
arcane ( for me ) ''string x = string.Copy(old_instance);'' rather than a
simple ''string x = new string(old_instance)''.




字符串是不可变的。因此,如果你复制它们,你基本上就是在浪费内存。

如果你能获得一份可变副本,复制只会很有趣。


布鲁诺。



Strings are immutable. So you are basically wasting memory if you copy them.
Copying is only interesting if you can get a mutable copy.

Bruno.


Cor写道:
嗨爱德华

VB.net
昏暗的新闻串as string = oldstring
C#
string newstring = oldstring;


这会将oldstring的引用分配给newstring。它不做

a复制。

C ++如你所说
string newstring = new string(oldstring)


语法错误。它必须是:


String * newstring = new String(oldstring);


我没看到像这样的构造函数在文档中。制作副本是否确实存在
?如果是这样的话没有记录。

我觉得你喜欢打字吗?


不,我不想打字。这就是我首先提出这个主题的原因。
我也发现在C ++中易于使用的复制构造函数。然而,你给C#的
给出的例子没有副本,而且C ++的更正示例没有。
存在AFAIK。我不知道VB,但我猜它不会复制

,而是创建另一个参考。

评论:
<从来没有尝试使用你用过的语言中旧的陈述。

例如,有人告诉我用他的语言他只用了50
代码进行排序,他翻译了那是我当时使用的那个和他的200行代码

他没有看到有一个政治家排序从一行。
(它也快得多)

<

Hi Edward

VB.net
dim newstring as string = oldstring
C#
string newstring = oldstring;
This assigns the reference of the oldstring to the newstring. It does not do
a copy.
C++ as you said
string newstring = new string(oldstring)
Wrong syntax. It would have to be:

String * newstring = new String(oldstring);

I don''t see a constructor like this in the documentation. Does it really
exist for making a copy ? If so it is not documented.

I think you love to type?
No, I hate to type. That is why I brought up the subject in the first place.
I also find copy constructors easy to use in C++. However the example you
gave for C# does not do a copy and the corrected example for C++ does not
exist AFAIK. I don''t know VB but I am guessing that it doesn''t do a copy
either but rather creates another reference.

Comments:

Never try to use your used old statements from you used language.

Example, once someone told me that in his language he only used 50
code for a sort, he had translated it to the one I was using then and
he 200 lines of code

He had not seen that there was a statemen "sort" from one line.
(It was also much faster)

Cor

来自C ++世界我无法理解为什么复制
构造函数不在.NET框架中使用的原因。复制构造函数
从另一个同类对象的副本创建一个对象。它听起来很简单但显然.NET出于某种原因难以理解这个概念。我确实理解.NET对象是在GC堆上创建的,但这并不意味着它们在创建时不能从另一个同类对象中复制。 />
我知道.NET有一个ICloneable接口,用于制作一个对象的深层副本,它甚至还有一个受保护的MemberWiseClone,可以在内部用于浅拷贝。但令我惊讶的是,ICloneable.Clone()有时候并不用来制作一个对象的深层副本,而是使用了一些其他特定的类成员
函数。

作为一个例子,当我查看system.string类时,我发现它没有复制构造函数,无论出于什么原因我不理解.NET似乎是正常的。然后我查看Clone()
函数,期望它复制字符串的内容并将其作为新字符串返回给我。不,它也不会这样做。
最后我找到了完成工作的Copy()函数。因此,不是从旧的字符串创建一个新的字符串,而是必须做奥术(对我而言)''字符串x = string.Copy(old_instance);''而不是简单的
''string x = new string(old_instance)''。

评论和解释?
Coming from the C++ world I can not understand the reason why copy
constructors are not used in the .NET framework. A copy constructor
creates an object from a copy of another object of the same kind. It
sounds simple but evidently .NET has difficulty with this concept
for some reason. I do understand that .NET objects are created on
the GC heap but that doesn''t mean that they couldn''t be copied from
another object of the same kind when they are created.

I understand that .NET has an ICloneable interface which is used to
make a deep copy of an object, and that it even has a protected
MemberWiseClone to be used internally for shallow copies. But much
to my surprise even ICloneable.Clone() is sometimes not used to make
a deep copy of an object but some other particular class member
function is used instead.

As an example when I look at the system.string class I find that it
does not have a copy constructor, which for whatever reason I do not
understand seems normal for .NET. Then I look at the Clone()
function, expecting it to make a copy of the string''s contents and
return it to me as a new string. No, it doesn''t do that either.
Finally I find the Copy() function which does the job. So instead of
creating a new string from an old one, I must do the arcane ( for me
) ''string x = string.Copy(old_instance);'' rather than a simple
''string x = new string(old_instance)''.

Comments and explanations ?



这篇关于复制构造函数和克隆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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