By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,320 Members | 2,198 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,320 IT Pros & Developers. It's quick & easy.

Is A.func1(param1).func2(param2.)func3.(param3) legal ???

P: n/a
If it is what is this technique called? A good example for this would
be when I need to extract a column out of a matrix, then create a new
diagonal matrix out of this column, ie:
A.cols(3).diag()
(cols() is a member function of class Matrix, returning a Vector
object. diag() is a member function of class Vector, returning a Matrix
object)
Is this the best syntax possible? What are the pros and cons (speedwise)

Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
bl**********@gmail.com wrote:
If it is what is this technique called?
"Chaining of member function calls".
A good example for this would
be when I need to extract a column out of a matrix, then create a new
diagonal matrix out of this column, ie:
A.cols(3).diag()
(cols() is a member function of class Matrix, returning a Vector
object. diag() is a member function of class Vector, returning a Matrix
object)
Is this the best syntax possible? What are the pros and cons (speedwise)


No cons. Just use it.

V
Jul 23 '05 #2

P: n/a
Yes, it is legal --- provided you put the dot operators in the right place.
Try

A.func1(param1).func2(param2).func3(param3);

<bl**********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com
If it is what is this technique called?
I am not aware that it has any particular name. The member function merely
has to return an object or a pointer/reference to an object and then a
member function can be called from the returned object/reference/pointer
(when a pointer is returned, you use -> rather than the dot operator).
A good example for this would
be when I need to extract a column out of a matrix, then create a new
diagonal matrix out of this column, ie:
A.cols(3).diag()
(cols() is a member function of class Matrix, returning a Vector
object. diag() is a member function of class Vector, returning a
Matrix object)
Is this the best syntax possible?
Yes.
What are the pros and cons (speedwise)


Compared to what? The speed will differ depending on whether an object is
returned or a reference/pointer. The syntax can be convenient, but gets
unreadable if the chain is too long.

--
John Carson

Jul 23 '05 #3

P: n/a

<bl**********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
If it is what is this technique called? A good example for this would
be when I need to extract a column out of a matrix, then create a new
diagonal matrix out of this column, ie:
I've heard it called "chaining" or "function chaining", but I don't think
there's any technical name for it.
A.cols(3).diag()
(cols() is a member function of class Matrix, returning a Vector
object. diag() is a member function of class Vector, returning a Matrix
object)
Is this the best syntax possible?


I'm not sure what you mean by the best, but, personally, while this is
perfectly legal, I tend not to use it, for two reasons.

First, it pust two different actions on one line, something I prefer not to
do (just as I prefer to have functions which only do their stated action,
not a series of actions). In this case, though, that would not really
matter so much to me, because the first action is really just an "accessor",
similar to doing A[3].diag().

But the second reason, and the main one for me, is that the syntax makes it
difficult to debug. Even though it takes longer to write and takes up more
storage (potentially, at least), I like to put returned values into
variables, and then use those variables. This makes debugging easier,
because now I have a named variable, something I can examine or put a watch
on, and a place I can set a breakpoint to see if I've got the data I expect
at that point. (The release build may even optimize the variable away, so
there's probably no real memory cost in the final releases.)

Just my personal practice. But no reason you can't do it the way you've
shown.

-Howard

Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.