473,670 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

The meaning of a = b in object oriented languages


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?

Sep 18 '07 #1
14 1449
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.
Sep 18 '07 #2
Summercool <Su************ @gmail.comwrite s:
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." —Albert Einstein |
Ben Finney
Sep 18 '07 #3
"Summercool " <Su************ @gmail.comwrote in message
news:11******** **************@ n39g2000hsh.goo glegroups.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.
Sep 18 '07 #4
On Sep 17, 11:04 pm, Lloyd Linklater <ll...@2live4.c omwrote:
SpringFlowers AutumnMoon wrote:
Is that the case: if a is an object, then b = a is only copying the
reference?

That and it adds a counter.

a = ["foo", "bar"]
b = a
b[0] = "bite me"
p a, b

a = "different"
p a, b

***

In the first print, we get
["something else", "bar"]
["something else", "bar"]

showing that changing b changes a, as expected. However, if we change
a, b is NOT changed as seen in the second print.

"different"
["something else", "bar"]

That means that there is a counter inside that says to separate the two
or b would have changed with a as a changed with b initially.
i think the line

a = "different"

means a is now set to a pointer to the String object with content
"different" .
or that "a is now a reference to the String object."

and b is still a reference to the Array object. so that's why a and b
print out different things. they point to different objects.

i think:

whenever in Ruby, Python, and Java,

a is never an object. a is always a "reference to an object"... this
will solve a lot of puzzles when we don't understand some code
behaviors.

when a writing or a book reads "a is a Hash object; a is an Array
object; or a is an Animal object" it is just a short form to say that
"a is a reference to that object."

b = a means "whatever a is referencing to, now b is referencing it
too".

so that's why a[1] = "foobar" will change what b will display, but
a = "foobar" will not change what b will display. (because a[1] =
"foobar" says "what is a referencing? go there and change its
content that has the index 1" and when b goes there to see it, it is
also changed.)
Sep 18 '07 #5
En Tue, 18 Sep 2007 03:57:36 -0300, Summercool <Su************ @gmail.com>
escribi�:
i think the line

a = "different"

means a is now set to a pointer to the String object with content
"different" .
or that "a is now a reference to the String object."

and b is still a reference to the Array object. so that's why a and b
print out different things. they point to different objects.

i think:

whenever in Ruby, Python, and Java,

a is never an object. a is always a "reference to an object"... this
will solve a lot of puzzles when we don't understand some code
behaviors.
Yes, but extrapolating that to "In OOPL, a=b just copies a reference" is
wrong.
"Old" languages like Fortran use the "boxed" model, and "modern" languages
tend to use the "reference" model, and since OO languages are younger...
But this rather old post by Alex Martelli explains it better
<http://mail.python.org/pipermail/python-list/2001-April/077544.html>

--
Gabriel Genellina

Sep 18 '07 #6
Summercool a écrit :
>

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

Oups, reading the subject I thought it was a Xah Lee post.
;-)
Sep 18 '07 #7
Laurent Pointal schreef:
Summercool a écrit :
>>
The meaning of a = b in object oriented languages.
============== =============== =============== ========
<zip>

Oups, reading the subject I thought it was a Xah Lee post.
me too ...

--
The saddest aspect of life right now is that science gathers knowledge
faster than society gathers wisdom.
-- Isaac Asimov

Roel Schroeven
Sep 18 '07 #8
In article <fc**********@n ews2.u-psud.fr>,
Laurent Pointal <la************ *@limsi.frwrote :
>Summercool a écrit :
>>
The meaning of a = b in object oriented languages.
============== =============== =============== ========
<zip>

Oups, reading the subject I thought it was a Xah Lee post.
....and you're perpetuating the impression by continuing the crossposting.
Please don't.
--
Aahz (aa**@pythoncra ft.com) <* http://www.pythoncraft.com/

The best way to get information on Usenet is not to ask a question, but
to post the wrong information.
Sep 18 '07 #9
Lew
Summercool wrote:
when a writing or a book reads "a is a Hash object; a is an Array
object; or a is an Animal object" it is just a short form to say that
"a is a reference to that object."

b = a means "whatever a is referencing to, now b is referencing it
too".

so that's why a[1] = "foobar" will change what b will display, but
a = "foobar" will not change what b will display.
You can't do both in Java. Is a an array or a String? If a is a String and b
is an array, then neither `a = b' nor `b = a' will compile in Java.

Java is a strongly-typed, compiled language which means it does more static
type checking and thus would reject treating a as both an array and a String.
In that environment the programmer must choose one or the other.

Otherwise what you say is exactly correct.
(because a[1] = "foobar" says "what is a referencing? go there and change its
content that has the index 1" and when b goes there to see it, it is
also changed.)
Speaking just of Java, it's useful to distinguish a variable from an object
(instance). As you point out, the variable represents a reference to the
instance. The variable has a compile-time type in Java, which may be
different from the run-time type of the object, albeit compatible.

C++ is similar in this respect. Python and Ruby are more, shall we say,
flexible in their type systems.

Both jet liners and hang gliders have their uses, both are flight, and neither
is really suitable for the other's purpose.

--
Lew
Sep 18 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1645
by: Benjamin C. Pierce | last post by:
The Twelth International Workshop on Foundations of Object-Oriented Languges (FOOL 12) Saturday 15 January 2005 Long Beach, California, USA Following POPL 05 The search for sound principles for object-oriented languages has given rise to much work on the theory of programming languages during the past two decades, leading to a better understanding of the key
5
2918
by: Martin | last post by:
When was inheritance intruduced into object oriented programming? More generally, does anyone know or have any sources on when the different features were introduced into object oriented programming?
14
2931
by: Rookie | last post by:
Is C an object oriented programming language?
8
2381
by: Dale | last post by:
I've searched Amazon and read probably 100 reviews but can't find what seems to be any book that is widely accepted as the definitive book on object oriented programming design and techniques. And most of the highest rated are all written 10 to 15 years ago. Any good suggestions?
47
5927
by: Thierry Chappuis | last post by:
Hi, I'm interested in techniques used to program in an object-oriented way using the C ANSI language. I'm studying the GObject library and Laurent Deniau's OOPC framework published on his web site at http://ldeniau.web.cern.ch/ldeniau/html/oopc/oopc.html. The approach is very instructive. I know that I could do much of this stuff with e.g. C++, but the intellectual challenge of implementing these concepts with pure ANSI C is relevant to...
46
3011
by: ajba74 | last post by:
Hi fellows, I am reading some books to learn the C programming language, and sometimes I have the feeling that when somebody becomes a C expert, he must learn a more modern and object-oriented language. When I read things like "... C++ is an evolution of C ..." or "... C is a subset of C++ ..." I tend to believe that I will have to learn C+ + sooner or later. It sounds like C++ is the future and C is the past (and will be no longer...
12
1483
by: Summercool | last post by:
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
3
1539
by: notnorwegian | last post by:
i have some confusion over this. sure a class is basically a classification, like for example an animal or flower. and an object/instance of that class is then for example a cat. an object is an instance of a class. that i know, i also know how to program with classes etc. i am just confused about the term object-oriented.
0
8469
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8903
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8814
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8592
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8661
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6213
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4391
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2042
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1794
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.