bugGNU Octave - Bugs: bug #42773, Operator Overloading of built-in...


bug #42773: Operator Overloading of built-in classes not working as expected

Submitted by:  dkeck <dkeck>
Submitted on:  Tue 15 Jul 2014 10:06:09 AM UTC  
Category: InterpreterSeverity: 3 - Normal
Priority: 5 - NormalItem Group: Matlab Compatibility
Status: PostponedAssigned to: None
Originator Name: Open/Closed: Open
Release: 3.8.0Operating System: Microsoft Windows

Add a New Comment(Rich Markup)

You are not logged in

Please log in, so followups can be emailed to you.


Wed 16 Jul 2014 03:38:22 PM UTC, comment #1:

In object-oriented programming there are two approaches to accomplish what you are trying to do. In approach 1, you take the base class and tweak one of it's methods through overloading. In approach 2 you inherit from a base class and extend the functionality of the base class with whatever behavior you want.

For core objects, Matlab and Octave are slightly different. Apparently Matlab treats its core objects as just another class and allows overloading. The same strategy does not work for Octave. I've changed the bug report Item Group to Matlab Compatibility to reflect that. But this is so deep in the internals of Octave that I don't see it being changed for years, if ever.

On the other hand, you can get the same behavior (a new definition for '+' which handles cell arrays) through approach 2.

In order to do that you just need a new class with a basic constructor that wraps a cell array and a new function 'plus' that implements your extended behavior.

I've attached an example class called @myclass. If you untar the directory somewhere and then start Octave in the directory above @myclass you should be able to see how it works.

Here is a sample running the example you posted.

Note that I did this quickly as a proof of concept. If you really go this direction then you should use a better class name and maybe put in some checks on what the inputs are, etc.

Rik <rik5>
Project Administrator
Tue 15 Jul 2014 10:06:09 AM UTC, original submission:

I put the plus.m [1] file into a folder called @cell under Octave root (/octave-3.8.0/@cell/plus.m) and added the @cell folder to the path.

Create two cell arrays and try to add them with '+' sign for testing:

As you can see the mapping of the operator to plus.m fails.

For more please see reference [2]


[2] http://octave.1599824.n4.nabble.com/Operator-Overloading-not-working-as-expected-td4665479.html

dkeck <dkeck>


(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach File(s):

Attached Files
file #31729:  @mycell.tgz added by rik5 (420B - application/x-gtar)


Depends on the following items: None found

Items that depend on this one: None found


Carbon-Copy List
  • -unavailable- added by rik5 (Posted a comment)
  • -unavailable- added by nul0m
  • -unavailable- added by dkeck (Submitted the item)

    Do you think this task is very important?
    If so, you can click here to add your encouragement to it.
    This task has 0 encouragements so far.

    Only project members can vote.


    Please enter the title of George Orwell's famous dystopian book (it's a date):



    Follow 6 latest changes.

    Date Changed By Updated Field Previous Value => Replaced By
    Wed 16 Jul 2014 03:39:27 PM UTCrik5Attached File-=>Added @mycell.tgz, #31729
    Wed 16 Jul 2014 03:38:22 PM UTCrik5CategoryNone=>Interpreter
      Item GroupNone=>Matlab Compatibility
      SummaryOperator Overloading not working as expected=>Operator Overloading of built-in classes not working as expected
    Tue 15 Jul 2014 04:33:49 PM UTCnul0mCarbon-Copy-=>Added -unavailable-

    Back to the top

    Powered by Savane 3.1-cleanup1