按值语义复制 [英] Copy by value symantics
问题描述
如何轻松地将一个结构分配给另一个不同类型的结构?
struct Object object = allocate_object();
struct Widget widget =(struct Widget)对象; / *不起作用* /
struct Widget比struct Object大。
Russell Shaw写道:
我如何轻松地将一个结构分配给另一个不同类型的结构?
struct Object object = allocate_object();
struct Widget widget =(struct Widget)对象; / *不起作用* /
struct Widget比struct Object大。
简短回答,可能不是你在看什么for:
memcpy(& widget,& object,sizeof widget);
答案很长:
>
通过单独复制每个成员
,这是最可靠的方法。示例:
struct Object object = allocate_object();
struct Widget小部件;
widget.wmember_one = object.omember_one;
widget.wmember_two = object.omember_two;
/ *以这种方式继续,直到你完成* /
更好的方法,恕我直言,将两个结构放入一个
联合,分配第一个,并从第二个读取;虽然
这可能是不合格的(有人会告诉我们
如果是:-)
union Both {
struct Object object;
struct Widget小部件;
};
struct Widget小部件;
Both.object = allocate_object();
widget = Both.widget;
Russell Shaw写道:
我如何轻松地将一个结构分配给另一个不同类型的结构?
struct Object object = allocate_object();
struct Widget widget =(struct Widget)object; / *不起作用* /
struct Widget比struct Object大。
为什么你会期望这样做做任何明智的事情?
如果`struct Widget''和`struct Object''不相关,
struct Object {
int this this,那个,the_other;
const char * canonical_name;
};
struct Widget {
struct Widget * link;
double centroid [3];
double coeff_matrix [4] [4];
ColorCode hue;
};
....作业有什么意义?
如果*是*某种
`struct Object''和`struct Widget'之间的关系,描述它是什么样的b $ b和某人可能有所帮助 - 但是对于任意
不相关类型的结构,任务很愚蠢。
-
Er ********* @ sun.com
< er ********* @ sun.com>写道:
如何轻松地将一个结构分配给另一个不同类型的结构?
struct Object object = allocate_object( );
struct Widget widget =(struct Widget)对象; / *不起作用* /
为什么你会期望这样做有意义呢?
我想这可能来自某种形式伪OO环境,你创建一个通用对象句柄,然后将它强制转换为某种类型。你在$ 32 $ b中一直在Win32 API中看到这个。
对于OP:这个分配是没有意义的。我怀疑你需要
分配指针,而不是对象?
struct Widget * p_widget =(struct Widget *)& object;
并记住这可能不合法C,因为
对象的成员与* p_widget的成员不一样。
-
Mark McIntyre
CLC FAQ< http://www.eskimo.com/~scs/C-faq/top.html>
CLC自述文件:< http://www.ungerhu.com/jxh/clc.welcome.txt>
---- ==通过新闻源发布。 Com - Unlimited-Uncensored-Secure Usenet News == ----
http:// www .newsfeeds.com 世界排名第一的新闻组服务! 120,000多个新闻组
---- =东海岸和西海岸服务器农场 - 通过加密实现全隐私= ----
Hi,
How do i easily assign one struct to another of a different type?
struct Object object = allocate_object();
struct Widget widget = (struct Widget)object; /* doesn''t work */
struct Widget is bigger than struct Object.
Russell Shaw wrote:Hi,
How do i easily assign one struct to another of a different type?
struct Object object = allocate_object();
struct Widget widget = (struct Widget)object; /* doesn''t work */
struct Widget is bigger than struct Object.
Short answer, possibly not what you are looking for:
memcpy (&widget, &object, sizeof widget);
Long answer:
The surest way of doing this by copying each member
individually. Example:
struct Object object = allocate_object ();
struct Widget widget;
widget.wmember_one = object.omember_one;
widget.wmember_two = object.omember_two;
/* go on in this fashion until you are done */
A better way, IMHO, is to place the two structs into a
union, assign the first, and read from the second; although
this is possibly non-conforming (someone will let us know
here if it is:-)
union Both {
struct Object object;
struct Widget widget;
};
struct Widget widget;
Both.object = allocate_object ();
widget = Both.widget;
Russell Shaw wrote:Hi,
How do i easily assign one struct to another of a different type?
struct Object object = allocate_object();
struct Widget widget = (struct Widget)object; /* doesn''t work */
struct Widget is bigger than struct Object.
Why would you expect this to do anything sensible?
If `struct Widget'' and `struct Object'' are unrelated,
struct Object {
int this, that, the_other;
const char *canonical_name;
};
struct Widget {
struct Widget *link;
double centroid[3];
double coeff_matrix[4][4];
ColorCode hue;
};
.... what meaning would the assignment have?
If there *is* some kind of relationship between
`struct Object'' and `struct Widget'', describe what it''s
like and someone may be able to help -- but for arbitrary
structs of unrelated types, assignment is silly.
--
Er*********@sun.com
On Mon, 06 Jun 2005 14:40:57 -0400, in comp.lang.c , Eric Sosman
<er*********@sun.com> wrote:
How do i easily assign one struct to another of a different type?
struct Object object = allocate_object();
struct Widget widget = (struct Widget)object; /* doesn''t work */
Why would you expect this to do anything sensible?
I imagine this arises from some sort of pseudo OO environment, where
you create a generic object handle, then coerce it to some type. You
see this all the time in the Win32 API.
To the OP: the assigment makes no sense. I suspect you need to be
assigning pointers, not objects?
struct Widget* p_widget = (struct Widget*) &object;
and bear in mind that this may not be legal C, since the members of
object will not be the same as those of *p_widget.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
这篇关于按值语义复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!