Thread: Subclassing and operator overloading

  1. #1
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002

    Subclassing and operator overloading

    I'm giving a small presentation on C++ to several Java people. One enthusiast I know will be attending hates C++ for all he's worth. I got in to an argument before with him about operator overloading and it's uses. I gave as an example, overloading the assignment operator can make copying instances of classes a lot easier in outside code. He argued that you would only have to subclass it in order to copy it. It's my understanding that subclassing (i think he means inheritence) will only create a new class, not a new instance of a class. Is his argument valid? The only way of copying an instance of a class (that I'm aware of) is to make a function to copy each element; add a little and you get operator overloading. One other argument I wanted to bring up at the presentation was that Java already has operator overloading, but you can't use it. (example of strings: "one " + "two "; ).
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    You and the Java programmer were on different tracks. You were talking about copying an instance of an object; he was talking about physically copying the code of a class, CTRL-C and CTRL-V (which can be avoided with inheritance).

    You're right, to make a copy of an object in Java, you basically have to write a method that does the copying; it might as well have a special name, operator=.

    So his argument was valid (you don't have to "copy" a class, you can use inheritance) but off track.

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    His argument was invalid in the sense that C++ has inheritance too. It was also invalid in that he obviously had no idea what you're talking about. This hate seems very irrational. (Not that language hating is ever rational. I freely admit that I'm irrational in hating VB.)
    All the buzzt!

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed