9 ms·
Thanks for this post; the importance of Alan Kay's work can hardly be overstated. Here's another quote by him that I find interesting: I invented the term Obje
by thebear 14y ago
Thanks for this post; the importance of Alan Kay's work can hardly be overstated. Here's another quote by him that I find interesting:
I invented the term Object-Oriented and I can tell you I did not have C++ in mind.
Whether or not you share the disdain for C++ that is implicit in this quote, I believe it is important to understand the difference between the way OO was originally conceived of and the way C++ interprets it. I believe the best way to understand the difference is to look at object-variable binding. Smalltalk and almost every other OO language that followed it uses reference semantics: the assignment
x = y
causes the variable x to release its binding to whatever object it is currently referring to, and to refer to the object that y is referring to instead. C++, on the other hand, is pretty much the only OO language that uses value semantics: the intended meaning of the assignment
x = y
is to copy the state of the object to which y is referring over to the object to which x is referring. The older and wiser I get the more I am inclined to believe that this attempt to marry OO and value semantics was an experiment that has failed. Even if you do not believe this, you'd have to admit that the collective amount of time and effort that the C++ community has spent on dealing with the assignment operator is simply staggering. (I would have made that last statement even two years ago, and now we have rvalue refereces.)
- alephnil 14y agoThe object model of C++ is almost entirely from Simula rather than from smalltalk. The object model of Simula is less dynamic and the language itself is based on Algol rather than being built from scratch like Smalltalk. In many ways, Simulas rather than Smalltalks object model has been the most influential, through C++ and later Java and C#.
- skrebbel 14y agoCan't be. Simula wasn't invented by Alan Kay.
- abc_lisper 14y agoYeah, but Java has primitives and thus value semantics you mention. Also, Java is much cleaner than c++ and on the scale of object-ness, java ranks higher than c++ but only a little bit.
- thebear 14y agoTrue, Java has primitives, but I believe it is pretty much decided that they will be discontinued beginning with Java 8. (The fact that the wrapper classes like Double and Integer are immutable makes them behave sort of as if they had value semantics, but that's really an orthogonal issue.)
- tharzoss 14y agoIt's funny that you want to see it as a flaw, while it's the only way that makes sense to me. You simply have a slight case of pointer-phobia. Languages like Python insist that there is a difference between basic data types and objects, that the former should copy by value, and the latter by reference. But what is a basic data type? Is a list (a builtin data type) basic? It turns out to be no (and it makes sense if you think of how C arrays are just pointers), and every python tutorial has to warn you of that. When are you dealing with reference, and when are you dealing with values? Expect to do some reading. While in C, we have actual pointers. Your reference is simply syntactic sugar when compared to actual pointers (and pointer to pointers). That means all C structs and basic data types can behave consistently (copying by value). Even pointers themselves are just fancy integers that copy by value. If you want to copy the references, use their addresses. C++ add classes, which are just fancier versions of structs (and thus similarly copy by value) and references, which are just pointers (that also make sure the objects exist). C++ also allows you to redefine the assignment operator itself, which I usually use to disallow copying complex objects by value (which can never hurt). I'm not scared of the power C++ gives me, but I understand many are, and thus I use it sparingly and document it carefully. Consistency, pointers, deterministic object destruction ... They're why C and C++ rule. You can make the case that they are too difficult for some programmers. But they are great for those who want clarity, control and performance.