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

B

bluekite2000

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)
 
V

Victor Bazarov

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
 
J

John Carson

Yes, it is legal --- provided you put the dot operators in the right place.
Try

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

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.
 
H

Howard

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top