复制对象和数组 [英] Copying objects and arrays

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

问题描述

好的,我是个新手,所以这个问题可能显得很傻。我正在使用

g ++ 3.3.x.

我被教导过一个数组与指针没什么不同

(实际上你可以使用指针算术来浏览它)。所以我想b $ b预计当我运行这个程序时,我得到c1.A和c2.A

指向相同的地址,并且更改c1.A意味着c2.A

也会改变。


----- BEGIN示例代码-----------


#include< iostream>

使用std :: cout;

使用std :: endl;


class C {

private:

int A [3];


public:

C(){for(int i = 0; i< 3; i ++)A [i] = 0; };

void alter(){A [1] ++; };

void printA()const;

};


void

C :: printA()const

{

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

cout<< " A [" <<我

<< 是 << A [i]

<<结束;

}


int

main()

{

C c1,c2;


c1.alter();


c2 = c1; //为什么这也会复制数组呢?


cout<< " c2是: << endl;

c2.printA();


c1.alter();

cout<< " c2仍然是: << endl;

c2.printA();


返回0;

}


----- END示例代码-----------


而是生成数组的副本,因此触摸c1.A不会不会影响

c2.A.


这是_c ++标准行为吗?我的幸运猜测是,当你使用默认的复制构造函数时,

赋值左边的对象会从一个对象的位置逐位复制内存。正确的

方,就像某种memcpy()一样。发生在两者之间。


因此,在c1中声明为自动变量的数组获取其内容

复制到新对象(c2)而不是简单地解决它指向的
,因为数组占用的所有空间都是按顺序分配在内存中的
以及在类范围内声明的其他变量。


希望我的英语足够可以理解......

Matteo

Ok, I''m quite a newbie, so this question may appear silly. I''m using
g++ 3.3.x.
I had been taught that an array isn''t a lot different from a pointer
(in fact you can use the pointer arithmetics to "browse" it). So I
expected that when I run this program, I get both c1.A and c2.A
pointing to the same address, and changing c1.A means that also c2.A
changes too.

----- BEGIN example CODE -----------

#include <iostream>
using std::cout;
using std::endl;

class C {
private :
int A[3];

public:
C() { for( int i=0; i<3; i++ ) A[i] = 0; };
void alter() { A[1]++; };
void printA() const;
};

void
C::printA() const
{
for( int i=0; i<3; i++ )
cout << "A[" << i
<< "] is " << A[i]
<< endl;
}

int
main( )
{
C c1, c2;

c1.alter();

c2 = c1; // Why does this also make a copy of the array??

cout << " c2 is : " << endl;
c2.printA();

c1.alter();
cout << " c2 is still : " << endl;
c2.printA();

return 0;
}

----- END example CODE -----------

Instead, a copy of the array is made, so touching c1.A doesn''t affect
c2.A.

Is this a _c++ standard_ behaviour? My lucky guess is that when you
use the default copy constructor, the object at the left of the
assignment gets the memory copied bit-to-bit from the one on the right
side, like a sort of "memcpy()" happens between the two.

So an array declared as an automatic variable in c1 gets its content
copied in the new object (c2) rather than simply the address it''s
pointing to, since all the space taken by the array is sequentially
allocated in memory along with other vars declared in the class scope.

Hope my english is understandable enough...
Matteo

推荐答案

" Matteo Settenvini < MS ********* @ tin.it>在消息中写道

news:8e ************************** @ posting.google.c om ...
"Matteo Settenvini" <ms*********@tin.it> wrote in message
news:8e**************************@posting.google.c om...
好的,我是个新手,所以这个问题可能显得很傻。我正在使用
g ++ 3.3.x.
我被教过一个数组与指针有很多不同


这是非常不同的。但是,在使用它们时使用相同的

语法的能力可能会产生差异。

(实际上你可以使用指针算术来浏览它)。


是的。

所以我希望当我运行这个程序时,我得到c1.A和c2.A
指向同一地址,


两者都没有指向任何地方。他们不是指针,他们是阵列。

他们不存储地址,他们存储整数。

和更改c1.A意味着也是c2 .A
也有变化。


不,他们是完全独立的对象。既不是c1

也不是c2包含任何指针。

----- BEGIN示例代码-----------

#include< iostream>
使用std :: cout;
使用std :: endl;

C类{
私人:
int A [3];

public:
C(){for(int i = 0; i< 3; i ++)A [i] = 0; };
void alter(){A [1] ++; };
void printA()const;
};

void
C :: printA()const
{
for(int i = 0; i <3; i ++)
cout<< " A [" <<我
<< 是 << A [i]
<< endl;
}
int
main()
{c / c c1,c2;

c1.alter() ;

c2 = c1; //为什么这也会复制数组?


它将对象''c1''的全部内容复制到

对象''c2''。先前的''c2''内容完全被覆盖了
。该数组是'

为什么被覆盖的内容的一部分。


因为你没有定义自己的分配运算符,

这与任何其他类型的作业完全相同。


cout<< " c2是: << endl;
c2.printA();

c1.alter();
cout<< " c2仍然是: << endl;
c2.printA();

返回0;
}
----- END示例代码------ -----

而是制作了阵列的副本,所以触摸c1.A并没有影响
c2.A.

是这是一个_c ++标准行为?


是的。你写的声明表达了:

"给''c2''与''c1''的值相同。

如果为<编写自己的赋值运算符br />
class''C'',然后你可以为''assign''定义一个新含义



我的幸运猜测是,当你使用默认的复制构造函数,


在上面的代码中调用复制ctor。

赋值从右侧的那个位置逐位复制内存,就像某种memcpy()一样。发生在两者之间。


差不多。


不是一点一滴,而是会员。这允许

的情况,其中一些成员是其他类类型的

可能有自己的创建和分配语义定义。

复制字节按字节会颠覆那些操作。

所以在c1
中声明为自动变量的数组

''c1''具有自动存储持续时间。这有效地给予其成员相同的持续时间,但仅限于''c1''。

因此,如果将''c1''定义为例如'c1''静态,''A''肯定是

而不是''自动''。


将其内容复制到新对象中(c2 )而不是简单地指向它的地址,


你的代码中有* *指针。 *没有*正在

''指向''。

因为数组占用的所有空间在内存中被顺序分配以及其他声明的vars在课堂范围内。

希望我的英语足够可以理解......
Ok, I''m quite a newbie, so this question may appear silly. I''m using
g++ 3.3.x.
I had been taught that an array isn''t a lot different from a pointer
It is very different. However the ability to use identical
syntax when using them can obsure the differences.
(in fact you can use the pointer arithmetics to "browse" it).
Yes.
So I
expected that when I run this program, I get both c1.A and c2.A
pointing to the same address,
Neither one points anywhere. They''re not pointers, they''re arrays.
They don''t store addresses, they store integers.
and changing c1.A means that also c2.A
changes too.
No. They''re completely separate objects. Neither c1
nor c2 contains any pointers.


----- BEGIN example CODE -----------

#include <iostream>
using std::cout;
using std::endl;

class C {
private :
int A[3];

public:
C() { for( int i=0; i<3; i++ ) A[i] = 0; };
void alter() { A[1]++; };
void printA() const;
};

void
C::printA() const
{
for( int i=0; i<3; i++ )
cout << "A[" << i
<< "] is " << A[i]
<< endl;
}

int
main( )
{
C c1, c2;

c1.alter();

c2 = c1; // Why does this also make a copy of the array??
It copies the entire contents of the object ''c1'' to the
object ''c2''. The previous content of ''c2'' is completely
overwritten. The array is part of the contents that''s
why it''s overwritten.

Since you''ve not defined your own assignement operator,
this works exactly like assignment for any other type.


cout << " c2 is : " << endl;
c2.printA();

c1.alter();
cout << " c2 is still : " << endl;
c2.printA();

return 0;
}

----- END example CODE -----------

Instead, a copy of the array is made, so touching c1.A doesn''t affect
c2.A.

Is this a _c++ standard_ behaviour?
Yes. You wrote the statement which expresses:
"give ''c2'' the same value as ''c1''.
If you write your own assignment operator for
class ''C'', then you can define a new meaning
for ''assign''.
My lucky guess is that when you
use the default copy constructor,
Do copy ctor is invoked in your code above.
the object at the left of the
assignment gets the memory copied bit-to-bit from the one on the right
side, like a sort of "memcpy()" happens between the two.
Almost.

Not bit by bit, but member by member. This allows for the
cases where some members are of other class types which
might have their own creation and assigment semantics defined.
Copying byte by byte would subvert those operations.
So an array declared as an automatic variable in c1
You''re using the wrong terminology. The array ''A'' is
a ''nonstatic data member''. It is its containing object,
''c1'' that has automatic storage duration. This effectively
gives its members the same duration, but only that of ''c1''.
So if ''c1'' were defined as e.g. static, ''A'' is certainly
not ''automatic''.

gets its content
copied in the new object (c2) rather than simply the address it''s
pointing to,
There are *no* pointers in your code. *Nothing* is being
''pointed to''.
since all the space taken by the array is sequentially
allocated in memory along with other vars declared in the class scope.

Hope my english is understandable enough...




我想知道你在写什么,我也认为

你不了解数组和/或指针。这个

是我和其他人告诉初学者避免使用标准容器的重要原因。例如。任何你可以使用std :: vector完成数组的



BTW谁在教你,哪本书是你的?学习?


-Mike



I think understand what you''re writing, and I also think
you don''t understand about arrays and/or pointers. This
is a big reason why I and others tell beginners to avoid
them and use the standard containers instead. E.g. anything
you can do with an array can be done with a std::vector.

BTW who is teaching you and which book(s) are you studying?

-Mike


Matteo Settenvini写道:
Matteo Settenvini wrote:
我被告知数组与指针有很多不同


你被教导错了。你应该找一个新老师。

数组和指针是完全不同的概念。他们

是不同的类型。用于浏览的指针。一个数组指向其中一个对象的
。加强这个结果的事情是从数组到指针的不明智的转换,它是第一个发生的成员。

c2 = c1; //为什么这也会复制数组?


是的,c1和c2每个都有一个名为A的int的三元素数组。

赋值将复制c1.A'的元素到c2.A.尽管

令人遗憾地缺少数组类型的赋值运算符,但每个

成员都会被复制它的赋值行为。


而是制作一个阵列的副本,所以触摸c1.A不会影响
c2


副本不是在赋值,C类型的每个对象都会在创建后立即声明

三元素数组。

这是一个_c ++标准行为吗?我的幸运猜测是,当你使用默认的复制构造函数时,
赋值左边的对象会从右边的那个位置逐位复制内存,像一种memcpy()发生在两者之间。


这里没有使用复制构造函数,发生的是隐式定义的

(我更喜欢说而不是默认值,因为默认默认为

cosntructor令人困惑)复制赋值操作符执行成员明智的

复制每个东西。它不是某种memcpy,每个成员都通过

复制它的赋值运算符(隐式或用户定义),并且在
$ b的情况下$ b数组,数组的每个成员都被复制。

因此,在c1中声明为自动变量的数组将其内容复制到新对象(c2)中而不是简单地复制解决它指向的问题,因为数组所占用的所有空间都是在内存中与在类范围内声明的其他变量一起顺序分配的。
I had been taught that an array isn''t a lot different from a pointer
You were taught wrong. You should get a new teacher.
Arrays and pointers are completely different concepts. They
are distinct types. A pointer used to "browse" an array points
at exactly one object within it. The thing that reinforces this
conclusion is an ill-advised conversion from array to pointer to
it''s first member that happens.

c2 = c1; // Why does this also make a copy of the array??
Yes, c1 and c2 each have a three element array of int called "A".
The assignment will copy c1.A''s elements to c2.A. This is despite
the unfortunate lack of an assignment operator for array types, each
member is copied with it''s assignment behavior.

Instead, a copy of the array is made, so touching c1.A doesn''t affect
c2
The copy isn''t made at the assignment, each object of type C gets the
three element array you declared as soon as it is created.

Is this a _c++ standard_ behaviour? My lucky guess is that when you
use the default copy constructor, the object at the left of the
assignment gets the memory copied bit-to-bit from the one on the right
side, like a sort of "memcpy()" happens between the two.
The copy constructor is NOT used here, what is happening is the implicitly
defined (I prefer to say that rather than default, because default default
cosntructor is confusing) copy-assignment operator performs a member wise
copy on each thing. It''s not "sort of memcpy", each member is copied via
it''s assignment operator (either implicit or user-defined) and in the case of
the array, each member of the array is copied.

So an array declared as an automatic variable in c1 gets its content
copied in the new object (c2) rather than simply the address it''s
pointing to, since all the space taken by the array is sequentially
allocated in memory along with other vars declared in the class scope.



数组不是自动变量。它是

类的成员变量。地址不会被复制,因为它不是指针。如果它是一个指针,那么将指定指针。这是一个数组,所以分配了

数组元素。



The array is NOT an automatic variable. It is a member variable of the
class. The address isn''t copied because it''s not a pointer. If it were
a pointer, then the pointers would be assigned. It''s an array, so the
array elements are assigned.


Matteo Settenvini写道:
Matteo Settenvini wrote:
Ok ,我是一个新手,所以这个问题可能显得很傻。我正在使用
g ++ 3.3.x.
我被教导过一个数组与指针没什么不同
(实际上你可以使用指针arithmetics来浏览它。所以我希望当我运行这个程序时,我得到c1.A和c2.A
指向相同的地址,并且更改c1.A意味着c2.A
更改。

----- BEGIN示例代码-----------

#include< iostream>
使用std: :cout;
使用std :: endl;

C类{
私人:
int A [3];

公众:
C(){for(int i = 0; i< 3; i ++)A [i] = 0; };
void alter(){A [1] ++; };
void printA()const;
};

void
C :: printA()const
{
for(int i = 0; i <3; i ++)
cout<< " A [" <<我
<< 是 << A [i]
<< endl;
}
int
main()
{c / c c1,c2;

c1.alter() ;

c2 = c1; //为什么这也会复制数组?


因为数组是成员。复制赋值适用于所有成员的分配

语义(不一定是赋值运算符)。

对于作为数组的成员,它意味着按元素进行复制赋值。

cout<< " c2是: << endl;
c2.printA();

c1.alter();


你正在改变c1对象的内容。

cout<< " c2仍然是: << endl;
c2.printA();

返回0;
}
----- END示例代码------ -----

而是制作一个阵列的副本,所以触摸c1.A并不影响
c2.A.


当然。 c1和c2是两个不同的对象。

这是_c ++标准行为吗?


这是标准任何OO语言的行为。任何[非静态]数据

成员都分配在为包含

的对象预留的内存中。

我的幸运猜测是当你使用默认的复制构造函数时,
赋值左边的对象会从右侧的那个位置逐位复制内存,就像一种" memcpy的()"发生在两者之间。


不,不是点对点。逐个成员,逐个元素。成员和

数组元素不一定是''int''。如果它们是其他类

对象,赋值语义将意味着为它们调用operator =。

所以在c1
中声明为自动变量的数组

它不是自动。它是_data_member_。

将其内容复制到新对象(c2)而不是简单地指向它的地址,因为所有空间都占用了数组按顺序分配在内存中以及在类范围内声明的其他变量。
Ok, I''m quite a newbie, so this question may appear silly. I''m using
g++ 3.3.x.
I had been taught that an array isn''t a lot different from a pointer
(in fact you can use the pointer arithmetics to "browse" it). So I
expected that when I run this program, I get both c1.A and c2.A
pointing to the same address, and changing c1.A means that also c2.A
changes too.

----- BEGIN example CODE -----------

#include <iostream>
using std::cout;
using std::endl;

class C {
private :
int A[3];

public:
C() { for( int i=0; i<3; i++ ) A[i] = 0; };
void alter() { A[1]++; };
void printA() const;
};

void
C::printA() const
{
for( int i=0; i<3; i++ )
cout << "A[" << i
<< "] is " << A[i]
<< endl;
}

int
main( )
{
C c1, c2;

c1.alter();

c2 = c1; // Why does this also make a copy of the array??
Because the array is a member. Copy assignment applies assignment
semantics (not necessarily the assignment operator) to all members.
For members that are arrays it means element-wise copy-assignment.

cout << " c2 is : " << endl;
c2.printA();

c1.alter();
You''re altering the contents of c1 object.
cout << " c2 is still : " << endl;
c2.printA();

return 0;
}

----- END example CODE -----------

Instead, a copy of the array is made, so touching c1.A doesn''t affect
c2.A.
Of course. c1 and c2 are two different objects.

Is this a _c++ standard_ behaviour?
It''s the "standard" behaviour of any OO language. Any [non-static] data
members are allocated in the memory set aside for the object that contains
them.
My lucky guess is that when you
use the default copy constructor, the object at the left of the
assignment gets the memory copied bit-to-bit from the one on the right
side, like a sort of "memcpy()" happens between the two.
No, not bit-to-bit. Member-by-member, element-by-element. Members and
elements of arrays are not necessarily ''int''. If they are other class
objects, assignment semantics will mean that operator= is called for them.

So an array declared as an automatic variable in c1
It''s not "automatic". It''s a _data_member_.
gets its content
copied in the new object (c2) rather than simply the address it''s
pointing to, since all the space taken by the array is sequentially
allocated in memory along with other vars declared in the class scope.




正确


V



Right

V


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

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