面向对象语言中a = b的含义 [英] The meaning of a = b in object oriented languages

查看:114
本文介绍了面向对象语言中a = b的含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



面向对象语言中a = b的含义。

==================== ============================== ==


我只想确认一下在OOP中,如果a是一个对象,那么b = a只是复制引用的




(使其成为最基本的形式:


a是4个字节,让我们说,在内存位置0x10000000到0x10000003


在0x10000000到0x10000003,它是值0xF0000000,指向

一个对象


b = a只是意味着

将4字节0xF0 0x00 0x00 0x00复制到0x20000000到0x2000003

这样b现在指向0xF0000000这是同一个对象。) />
所以基本上,a只是一个指向对象的指针。


和b = a只意味着将相同的指针放入b。


这就是为什么在Python或Ruby中,它就像:


>> a = {" a" :1,b :2}
b = a a



{'''':1,'' b'':2}


>> b



{'''':1,''b'':2}


>> a [" a"] = 999
a



{''a'':999,'b'':2}


>> b



{''a'':999,''b'' :2}


所以大多数或所有面向对象的语言通过引用进行分配?

是否有任何面向对象的语言实际上通过
$进行分配b $ b值?我有点记得在C ++中,如果你这样做的话。


动物a,b;


a = b实际上是按值分配。 />
而在Java,Python和Ruby中,所有赋值都由

引用。 (按引用设置)


是这样的情况:如果a是一个对象,那么b = a只是复制了

引用? br />

解决方案

Summercool写道:


所以大多数或所有面向对象的语言通过引用进行分配?

是否有任何面向对象的语言实际上通过

值进行分配?我有点记得在C ++中,如果你这样做的话。


动物a,b;


a = b实际上是按值分配。 />
而在Java,Python和Ruby中,所有赋值都由

引用。 (按引用设置)


是这样的情况:如果a是一个对象,那么b = a只是复制

引用?



是的,您的理解完全正确;除非你明确使用指针,否则C ++将按值分配

,但其他语言将通过引用分配

(原始类型除外)。


-

始终关注生活的光明面。

要通过电子邮件回复,请将no.spam替换为我的姓氏。


Summercool< Su ************ @ gmail.comwrites:


我只想确认在OOP中,如果a是一个对象,那么b = a只是复制引用的




语言是否为OO与此问题无关。赋值运算符的

语义在

语言之间可以有所不同,与是否涉及OOP正交。


-

\我们的任务一定是通过扩大我们的监狱来扩大我们自己的监狱|

` \我们的同情圈,拥抱全人类和整个人。 />
_o__)大自然的美。阿尔伯特爱因斯坦|

Ben Finney


" Summercool" < Su ************ @ gmail.com在留言中写道

新闻:11 ****************** ****@n39g2000hsh.googlegr oups.com ...


>

面向对象语言中a = b的含义。

========================================= ========= ==


我只是想在OOP中确认,如果a是一个对象,那么b = a是

只复制引用。


(使其成为最基本的形式:


a是4字节,让我们说,在内存位置0x10000000到0x10000003

b是4个字节,让我们说,在内存位置0x20000000到0x20000003


在0x10000000到0x10000003 ,它是值0xF0000000,指向

一个对象


b =一个只是意味着

复制4个字节0xF0 0x00 0x00 0x00进入0x20000000到0x2000003

这样b现在指向0xF0000000这是同一个对象。)


所以基本上,a只是一个指向一个对象的指针。


和b = a只是意味着将同一个指针放入b。


这就是为什么在Python或Ruby,它就像:


>>> a = {" a" :1,b :2}
b = a
a



{''''':1,''b'': 2}


>>> b



{''a'':1,''b'':2}


>>> a ["] = 999
a



{''''':999,''b'':2 }


>>> b



{''a'':999,'b'':2}


所以大多数或所有面向对象的语言都通过引用进行分配?

有没有任何面向对象的语言实际上通过

值进行分配?我有点记得在C ++中,如果你这样做的话。


动物a,b;


a = b实际上是按值分配。 />
而在Java,Python和Ruby中,所有赋值都由

引用。 (按引用设置)


是这样的情况:如果a是一个对象,那么b = a只是复制

引用?



在C ++中,默认赋值构造函数与

默认复制构造函数几乎相同,有时也称为按位复制,但

并非严格属实。对于POD类型(普通旧数据)你是什么

显示是真的,它是一个按位复制,非常类似于memcpy(目的地,

源,sizeof(目的地) ))。但是,对于非POD类型,这不是真的,因为类或结构中的对象将调用它们的赋值运算符,并且它们可能被覆盖。一个主要的例子是

std :: string。如果std :: string成员被按位复制,那么就会有两个std :: string指向相同内存位置的实例

(因为std ::) string通常通过指针存储字符串数据。


C ++中的赋值运算符应该尝试阻止两个指针将
置于相同的内存位置。 Consier一个简单的课程(未经测试):


class Foo

{

public:

char *数据;

int DataSize;

Foo(int Size):DataSize(Size){Data = new char [Size]; }

~Foo(){删除数据[]; }

};


现在,如果我们离开课堂,我们会遇到问题。默认副本

构造函数和赋值运算符将对指针执行按位复制

数据。 IE


int main()

{

Foo bar1(10);

Foo bar2 (20);

bar2 = bar1; //很多问题

}


首先,默认赋值运算符只需复制指针来自

bar1(其中指向bar2,覆盖bar2'。由于

我们不再有指向bar2数据的指针,我们无法删除它,

导致内存泄漏。此外,此时bar1和bar2'的数据指针

指向相同的内存位置。改变一个的内容会改变另一个的内容,因为它们是同一个的内容。另外,当调用

析构函数时,两者都会尝试删除[]相同的指针,

第一个将成功,第二个将导致错误,因为指针

已被释放。所以我们需要覆盖复制构造函数和

赋值运算符来解决这个问题。所以我们加入Foo:


Foo& operator =(const Foo& rhs)

{

delete []数据;

数据=新字符[rhs.DataSize];

memcpy(Data,rhs.Data,rhs.DataSize);

DataSize = rhs.DataSize;

}


你可以看到我们必须手动做一些事情。我们必须删除[]

我们的指针,新的缓冲区,复制cotents,复制DataSize,

,默认赋值运算符都不会完成。副本

构造函数类似,我们不必删除[]数据;

因为还没有分配任何内容。

很明显,如果您尝试使用
进行编译,我在此处显示的代码可能存在错误。预先警告。



The meaning of a = b in object oriented languages.
================================================== ==

I just want to confirm that in OOP, if a is an object, then b = a is
only copying the reference.

(to make it to the most basic form:

a is 4 bytes, let''s say, at memory location 0x10000000 to 0x10000003

b is 4 bytes, let''s say, at memory location 0x20000000 to 0x20000003

in 0x10000000 to 0x10000003, it is the value 0xF0000000, pointing to
an object

b = a just means
copy the 4 bytes 0xF0 0x00 0x00 0x00 into 0x20000000 to 0x2000003
so that b now points to 0xF0000000 which is the same object.)
so essentially, a is just a pointer to an object.

and b = a just means that put that same pointer into b.

and that''s why in Python or Ruby, it is like:

>>a = {"a" : 1, "b" : 2}
b = a
a

{''a'': 1, ''b'': 2}

>>b

{''a'': 1, ''b'': 2}

>>a["a"] = 999
a

{''a'': 999, ''b'': 2}

>>b

{''a'': 999, ''b'': 2}

so most or all object oriented language do assignment by reference?
is there any object oriented language actually do assignment by
value? I kind of remember in C++, if you do

Animal a, b;

a = b will actually be assignment by value.
while in Java, Python, and Ruby, there are all assignment by
reference. ("set by reference")

Is that the case: if a is an object, then b = a is only copying the
reference?

解决方案

Summercool wrote:

so most or all object oriented language do assignment by reference?
is there any object oriented language actually do assignment by
value? I kind of remember in C++, if you do

Animal a, b;

a = b will actually be assignment by value.
while in Java, Python, and Ruby, there are all assignment by
reference. ("set by reference")

Is that the case: if a is an object, then b = a is only copying the
reference?

Yes, your understanding is exactly correct; C++ will assign by value
unless you explicitly use pointers, but the other languages will assign
by reference (except for primitive types).

--
"Always look on the bright side of life."
To reply by email, replace no.spam with my last name.


Summercool <Su************@gmail.comwrites:

I just want to confirm that in OOP, if a is an object, then b = a is
only copying the reference.

Whether the language is OO or not has no bearing on this question. The
semantics of the assignment operator can and do differ between
languages, orthogonal to whether OOP is involved.

--
\ "Our task must be to free ourselves from our prison by widening |
`\ our circle of compassion to embrace all humanity and the whole |
_o__) of nature in its beauty." a??Albert Einstein |
Ben Finney


"Summercool" <Su************@gmail.comwrote in message
news:11**********************@n39g2000hsh.googlegr oups.com...

>
The meaning of a = b in object oriented languages.
================================================== ==

I just want to confirm that in OOP, if a is an object, then b = a is
only copying the reference.

(to make it to the most basic form:

a is 4 bytes, let''s say, at memory location 0x10000000 to 0x10000003

b is 4 bytes, let''s say, at memory location 0x20000000 to 0x20000003

in 0x10000000 to 0x10000003, it is the value 0xF0000000, pointing to
an object

b = a just means
copy the 4 bytes 0xF0 0x00 0x00 0x00 into 0x20000000 to 0x2000003
so that b now points to 0xF0000000 which is the same object.)

so essentially, a is just a pointer to an object.

and b = a just means that put that same pointer into b.

and that''s why in Python or Ruby, it is like:

>>>a = {"a" : 1, "b" : 2}
b = a
a

{''a'': 1, ''b'': 2}

>>>b

{''a'': 1, ''b'': 2}

>>>a["a"] = 999
a

{''a'': 999, ''b'': 2}

>>>b

{''a'': 999, ''b'': 2}

so most or all object oriented language do assignment by reference?
is there any object oriented language actually do assignment by
value? I kind of remember in C++, if you do

Animal a, b;

a = b will actually be assignment by value.
while in Java, Python, and Ruby, there are all assignment by
reference. ("set by reference")

Is that the case: if a is an object, then b = a is only copying the
reference?

In C++ the default assignment constructor is virtually the same as the
default copy constructor, which is sometimes called a bitwise copy, although
that is not strictly true. For POD types (Plain Old Data) what you are
showing is true, it''s a bitwise copy, very similar to memcpy( destination,
source, sizeof( destination) ). For non POD types, however, that is not
true as objects inside the class or structure will have their assignment
operators called, and they may be overridden. A prime example of this is
std::string. If the std::string member was bitwise copied, then there would
be two instances of a std::string pointing to the same memory locations
(since std::string typically stores the strings data via a pointer).

Assignment operators in C++ should attempt to prevent two pointers poining
to the same memory location. Consier a simple class (untested):

class Foo
{
public:
char* Data;
int DataSize;
Foo( int Size ): DataSize( Size ) { Data = new char[Size]; }
~Foo() { delete Data[]; }
};

Now, if we leave the class at this, we get into problems. The default copy
constructor and assignment operators will do a bitwise copy on the pointer
Data. I.E.

int main()
{
Foo bar1( 10 );
Foo bar2( 20 );
bar2 = bar1; // Lots of problems
}

First off, the default assignment operator will simply copy the pointer from
bar1 (which points to 10 characters) into bar2, overwriting bar2''s. Since
we no longer have a pointer to the data from bar2 we can not delete it,
causing a memory leak. Also, at this point bar1 and bar2''s Data pointers
point to the same memory location. Changing the contents of one will change
the contents of the other, since they are one in the same. Also, when the
destructors are called, both will attempt to delete[] the same pointer, the
first one will succeed, the second one will cause an error as the pointer
has already been freed. So we need to override the copy constructor and
assignment operators to fix this. So we add to Foo:

Foo& operator=( const Foo& rhs )
{
delete[] Data;
Data = new char[rhs.DataSize];
memcpy( Data, rhs.Data, rhs.DataSize );
DataSize = rhs.DataSize;
}

You can see that we have to manually do some things. We have to delete[]
our pointer, new a new buffer, copy the cotents, copy the DataSize over,
none of which the default assignment operator would of done. The copy
constructor would be similar, we just wouldn''t have to delete[] Data;
because nothing has been allocated yet.

Incidently, there may be errors in the code I''ve shown here if you attempt
to compile it. Be forewarned.


这篇关于面向对象语言中a = b的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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