Re: The meaning of a = b in object oriented languages
"Summercool" <Summercoolness@gmail.com> wrote in message
news:1190094057.976729.285120@n39g2000hsh.googlegroups.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}
{'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 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.