help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

OOP: inheriting from other classes


From: Søren Hauberg
Subject: OOP: inheriting from other classes
Date: Sat, 26 Jul 2008 12:18:01 +0200

Hi,
  I've been looking a little bit at the new object oriented features of
the 3.1.x series, and I'm having some trouble understanding the system.
  Specifically I would like to create a 'constant' class, where I only
overwrite the 'subsasgn' function. That is, I'd like to be able to do
something like this:

  a = rand (3, 3);
  c = constant (a);
  c (2, 2)

which should return the same thing as 'a (2, 2)'. But I want

  c (2, 2) = 1

to return an error. Now, I can create a constructor that looks like
this:

  function retval = constant (data)
    if (nargin == 0)
      data = struct ("data", []);
    endif
  
    if (isa (data, "constant"))
      retval = data;
    else
      retval.data = data;
      retval = class (retval, "constant");
    endif
  endfunction

and a 'subsasgn' function like this:

  function subsasgn (c, varargin)
    error ("cannot change constant variable");
  endfunction

But if I want a 'constant' object to behave just like the input argument
of the constructor, then it seems I have to implement a version of every
function I could ever image calling, which would take an infinite amount
of time (and I'm a bit stretched for time atm).

So, I guess my question is, how can I do inheritance? Is it possible to
create a class that that inherits from the input arguments to the
constructor?

Søren



reply via email to

[Prev in Thread] Current Thread [Next in Thread]