help-octave
[Top][All Lists]
Advanced

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

Re: Did I miss something about structures and function handles


From: kensmith
Subject: Re: Did I miss something about structures and function handles
Date: Sat, 18 Aug 2007 14:16:04 -0700
User-agent: KMail/1.9.1

On Saturday 18 August 2007 13:13, James Sherman Jr. wrote:
> I think he means if the structure has fields, xx, yy, and f,
> something like b.xx = 10;
> b.yy = 7;
> could he write a function f like this:
> b.f=@(X)(xx+yy+X)
>
> where xx and yy refer to the other fields in the b structure
> (something like this->xx if this was in C++), which I don't think is
> possible in octave. But I don't have a good grasp of what OOP really
> and truly means, so the 90% of OOP comment by kensmith confuses me. 
> I usually think of it in terms of could be done in C (not OOP) vs.
> C++ (is OOP), but everything kensmith wrote in his email seems to not
> be OOP to me.  It is just a struct with a field that is a function
> (or function pointer).  I could be way off base though.

You correctly identified the missing ability.  Let me explain a little 
about the details of OO programming and I think you will see what I 
mean.

The programmers view of what is going on in an OO program is largely  a 
useful fiction.  In C++ much of the work is done at compile time.  When 
you call a (nonvirtual) member function, the compiler does something 
like this.

Y=TheCircle.FindRadius();

Becomes:

Y = CircleClass_FindRadius(TheCircle);

The "this" variable inside a member function is an extra passed 
parameter.  The function that gets called in this case is just an 
ordinary function with a name that was invented by the compiler.  

To the programmer, the member function appears to be within the variable 
because all of the references to other members of the same variable are 
referenced using the "this" passed parameter.  It is a handy thing for 
programming but really it is just a trick the compiler is doing.

Virtual functions are quite a different matter.  Any variable you create 
of a class that has virtual functions has an extra element in it.  This 
element points to a thing called the Virtual Method Table.  A call to a 
virtual method works a little like this:

       Y=TheCircle.FindRadius() ;

becomes:

    Y = TheCircle.VMT(IndexOf_CircleClass_FindRadius)(TheCircle);

It looks up the address of the function to use in the VMT and then calls 
it with TheCircle as the "this" parameter.

Java Script does things quite differently.  It is a better model for 
what I was suggesting as a way to do objects in octave.  In JavaScript, 
things are typed on the fly like in octave.  Members of a structure can 
be functions (more or less really) in JavaScript.  I am suggesting that 
in octave, the way to do it is to make the members references to the 
functions.  I didn't bother to make it a double dereference like the 
VMT system would.  This would cost some overhead on size so perhaps you 
can do that if you want to.

As I see it, there is no problem with having to code the methods of a 
type with some explicit indication that the variables being used are 
other members of the same variable.  This means that the only missing 
thing is this ability to reference the other members.  If someone know 
of a way, then I would have fully OO coding in octave.

>
> James
>
> On 8/18/07, Matthias Brennwald <address@hidden> wrote:
> > On 18.08.2007, at 19:04, address@hidden wrote:
> > > I can say:
> > >
> > > b.a=@(X)(disp(X-3))
> > >
> > > if I then say
> > >
> > > c = b
> > > c.a(7)
> > >
> > > I get 4 as expected.
> > >
> > > This looks to me like 90% of object oriented programming is in
> > > octave. Is there some way that I can get at the other elements of
> > > "b" and "c" from the member "a".
> >
> > Which 'other elements' of b and c do you mean? In your example
> > above, I can see only one element (a) in b and c.
> >
> > Matthias
> >
> >
> > -------
> > Matthias Brennwald
> > Lägernstrasse 6
> > CH 8037 Zürich
> > +41 (0)44 364 17 03
> > address@hidden
> >
> >
> >
> > _______________________________________________
> > Help-octave mailing list
> > address@hidden
> > https://www.cae.wisc.edu/mailman/listinfo/help-octave

-- 
address@hidden



reply via email to

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