C vs C++ vs Lisp (156)

92 Name: #!/usr/bin/anonymous : 2008-08-13 04:09 ID:svxdzyWV

>>90

Is paint a static method of car? Cars don't paint themselves so having a paint member on car wouldn't make sense. In a proper model, car would be part of some carFactory class or some helper function in an appropriate namespace. In OOP just because you want to do something to an object does not always mean that class should be the one doing it.

>How can they tell if a referenced variable will change? Here's a method call: ...

I can tell you that the bar member will use baz (I mean really it should) and can modify it. To know if it does change you do of course have to inspect bar. To see everywhere that baz will change you have to inspect all the functions that have baz in the signature (or the scope its created in of course). Now if we have a global, it could be changed in bar. It could be changed anywhere. I have no idea where in the program that global is going to be used, it can be used and changed anywhere.

Changes to the reference are limited and easy to indentify where they are potentially going to happen. Changes to globals can happen anywhere and the entire code needs to be inspected.

>By comparison, if it's a value, you know if baz will change: since you haven't used assignment here, no.

The problem with the value of baz is baz is now not the object you passed in.

>>91

A reference variable is one that evaluates to a pointer. Your examples are true enough, but what I am talking about is objects and OOP.

In VB, VB.Net, C#, Java and some other languages there are 2 types of variables. Value types and reference types. Value types evaluate to the value of the variable (things like numbers and strings) and reference types evaluate to a pointer to the value (all object variables).

In VB you would never want to do this:
Sub Foo(ByRef X As Object)

X = New Object

End Sub

What you have just told it to do is pass a reference to the reference passed in. What you want is the value of the variable to pass in because all object variables are references. So you would want the signature to read: "ByVal X As Object" to get the reference to the object being passed. (The VB compiler actually isn't that dumb and will treat the above function as if the X variable is passed by value automatically and strangley without a warning).

Value type variables can be passed either by the value or by a reference to it.

The distinction between the 2 types of variables makes OOP easier and less prone to mistakes as the language and compiler treat the variables properly. C++ does not can cannot have this distinction so it is up to you to add the etra syntax to properley work the OOP way.

Also, all variables in Python are references.

This thread has been closed. You cannot post in this thread any longer.