很明显,如果您尝试使用进行编译,我在此处显示的代码可能存在错误。预先警告。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 isonly copying the reference.(to make it to the most basic form:a is 4 bytes, let''s say, at memory location 0x10000000 to 0x10000003b is 4 bytes, let''s say, at memory location 0x20000000 to 0x20000003in 0x10000000 to 0x10000003, it is the value 0xF0000000, pointing toan objectb = a just meanscopy the 4 bytes 0xF0 0x00 0x00 0x00 into 0x20000000 to 0x2000003so 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'': 1, ''b'': 2}{''a'': 1, ''b'': 2}{''a'': 999, ''b'': 2}{''a'': 999, ''b'': 2}so most or all object oriented language do assignment by reference?is there any object oriented language actually do assignment byvalue? I kind of remember in C++, if you doAnimal a, b;a = b will actually be assignment by value.while in Java, Python, and Ruby, there are all assignment byreference. ("set by reference")Is that the case: if a is an object, then b = a is only copying thereference? 解决方案Yes, your understanding is exactly correct; C++ will assign by valueunless you explicitly use pointers, but the other languages will assignby reference (except for primitive types).--"Always look on the bright side of life."To reply by email, replace no.spam with my last name.Whether the language is OO or not has no bearing on this question. Thesemantics of the assignment operator can and do differ betweenlanguages, 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{''a'': 1, ''b'': 2}{''a'': 1, ''b'': 2}{''a'': 999, ''b'': 2}{''a'': 999, ''b'': 2}so most or all object oriented language do assignment by reference?is there any object oriented language actually do assignment byvalue? I kind of remember in C++, if you doAnimal a, b;a = b will actually be assignment by value.while in Java, Python, and Ruby, there are all assignment byreference. ("set by reference")Is that the case: if a is an object, then b = a is only copying thereference?In C++ the default assignment constructor is virtually the same as thedefault copy constructor, which is sometimes called a bitwise copy, althoughthat is not strictly true. For POD types (Plain Old Data) what you areshowing is true, it''s a bitwise copy, very similar to memcpy( destination,source, sizeof( destination) ). For non POD types, however, that is nottrue as objects inside the class or structure will have their assignmentoperators called, and they may be overridden. A prime example of this isstd::string. If the std::string member was bitwise copied, then there wouldbe 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 poiningto 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 copyconstructor and assignment operators will do a bitwise copy on the pointerData. 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 frombar1 (which points to 10 characters) into bar2, overwriting bar2''s. Sincewe 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 pointerspoint to the same memory location. Changing the contents of one will changethe contents of the other, since they are one in the same. Also, when thedestructors are called, both will attempt to delete[] the same pointer, thefirst one will succeed, the second one will cause an error as the pointerhas already been freed. So we need to override the copy constructor andassignment 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 copyconstructor 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 attemptto compile it. Be forewarned. 这篇关于面向对象语言中a = b的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!