help-octave
[Top][All Lists]
Advanced

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

Re: creating another object of a given class


From: CdeMills
Subject: Re: creating another object of a given class
Date: Thu, 23 Dec 2010 03:06:34 -0800 (PST)


Steve White wrote:
> 
> 
> What is the best way to create another object of a given class?
> 
> Say a function takes an object as an argument
> and a new object of that class is needed.
> 
> One way to do it (for an object 'obj') is
> 
>     name = class (obj);
>     new_obj = feval (name, args);
> 
> However, the Coding-Tips documentation entry says to avoid calling 
> feval "excessively":
> http://www.gnu.org/software/octave/doc/interpreter/Coding-Tips.html#Coding-Tips
> 
> Another option would be to attach a function handle to the
> constructor of each class as an object member, and call that
> when needed.  But this seems cludgy.
> 
> Is there a better/faster/accepted way to do this?
> 
> 

It depends. If you implement my_object(some vals), through overloaded
'subsref' method, as a call to the object constructor, this would achieve
your goal. Now you're using the usual dereferencing as a member call, which
is counter-intuitive. If you want to stay with the syntax
my_object(some val) 
returning a sub-portion of your object, then I don't see other options than
to use feval. Be sure to enclose it it a try/catch block, in case the the
constructor for your class is not known.

The first option would be OK if the language had different syntax for
function calls and dereferencing, which is not the case in Octave. For
instance, R has () for function call and [] for dereferencing, but then it
has its own function for concatenation, as 
x = [ 1, 2, 3] 
is no more a valid construct.

Regards

Pascal

-- 
View this message in context: 
http://octave.1599824.n4.nabble.com/creating-another-object-of-a-given-class-tp3161399p3161908.html
Sent from the Octave - General mailing list archive at Nabble.com.


reply via email to

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