What changes for C++ programmers in NR3?


danielnet
10-19-2007, 04:05 AM
Hello,

just yesterday I learned about the existence of NR3.
About two years ago I purchased "NR in C++, second edition", and as a C++ programmer I am of course very happy that the new NR3 comes with C++ code.
My question now is if (beside of the new chapters and sections included in NR3) there are any changes regarding the organization of the code with respect to "NR in C++"? It would be great, if N3R would be more organized in the style of the STL, e.g. using iterators and function objects...(instead of references to containers, and function pointers...)

Thanks
Daniel

Bill Press
10-19-2007, 09:57 PM
Hello, Daniel.
The C++ code in NR3 is quite different from that in NR2. The old version's C++ was basically a wrapper around what was still basically C code. The new version is much more object oriented and, at least in parts, has a lot of class derivation, use of templates, functors, etc. However, to answer your specific question, there is very little use of STL (containers, iterators, etc.). The reason for this conscious choice was that we needed the C++ also to serve as a vehicle for showing concepts that some users might implement in other languages, and we thought that heavy use of STL constructs would interfere with this. You might of course disagree with that decision, but that is how it came out.
Cheers,
Bill P.

danielnet
10-22-2007, 06:07 AM
Hello Bill,

thank you for your quick answer. I understand your decision about not using STL constructs too much. I appreciate the use of templates and functors. Object orientated design is neat for many application. But the disadvantage of class inheritance is the call of virtual functions which results in slower code (runtime decisions, instead of compile time decisions) and higher memory consumption (creation of virtual function tables). Since I have not seen the N3R code yet, I do not know how you cope with this.
It would be very useful if one could download the complete first chapter of NH3 from your site as it was possible for the first chapter of NRC++2d (as far as I remember). This should be useful also for non C++ programmers, because then they could judge on their own how easy or difficult it will be to implement the recipes in their favorite language, or if they are better served buying one of the still available "legacy" editions.

Regards,

Daniel

Chip
11-11-2007, 06:32 AM
Hi Daniel

I just got my hardcopy of NR3. A very nice and attractive book. The C++ usage is very straight and readable, for good reasons given by the authors. The code reads almost like pseudocode and would work very nicely as prototypes for your own (possibly more advanced) use of OO features available in C++. The nr3.h header file is also very readable, even if it makes heavy use of templates. I suggest you read the authors introduction and philosophy regarding OO and C++. By the way, if you haven't bought the book yet, I think it very good value for money!

Greetings
Chip

danielnet
11-13-2007, 10:37 AM
Hi Chip,

thank you for your post. I have ordered the book already and wait eagerly for it. (Unfortunately there seems to be a supply shortage here in Switzerland...)

I still think it would be helpful for many people if the first chapter of the book was available as free download (as it was for "NR in C++").

Regards
Daniel

Bill Press
11-13-2007, 05:50 PM
Hello, Daniel.
Right now, the first section of every chapter is available for free, plus all of Chapter 3. We decided to make Chapter 3 the sample chapter, instead of Chapter 1, because it is more typical of what the book is like. I can see the logic, however, of having Chapter 1 be available for people wondering how we handle various C++ issues. I'll consult our publisher and see if they would let us add Chapter 1 to the free list.
The way to see the free sections is to go to http://www.nrbook.com and follow the "guest" directions there. (You'll have to install the FileOpen plug-in, as discussed in other threads in this forum.)
Cheers,
Bill P.

yohm
12-30-2007, 02:00 PM
Hello Bill,

thank you for your quick answer. I understand your decision about not using STL constructs too much. I appreciate the use of templates and functors. Object orientated design is neat for many application. But the disadvantage of class inheritance is the call of virtual functions which results in slower code (runtime decisions, instead of compile time decisions) and higher memory consumption (creation of virtual function tables). Since I have not seen the N3R code yet, I do not know how you cope with this.
It would be very useful if one could download the complete first chapter of NH3 from your site as it was possible for the first chapter of NRC++2d (as far as I remember). This should be useful also for non C++ programmers, because then they could judge on their own how easy or difficult it will be to implement the recipes in their favorite language, or if they are better served buying one of the still available "legacy" editions.

Regards,

Daniel

Hello Daniel,
I just read your concern about the efficiency of code that makes use of virtual functions and had a look at the sample chapter of NR3; To my surprise there actually are virtual functions.
I am aware that the authors NR3 had a choice to make either in favor of speed or flexibility, and appearently flexibility won the poll. That choice also has the advantage that the code can be written in "main stream" C++.
If one is in desperate need of ultimate speed and does not need runtime flexibility, then some C++ trickery is necessary of which passing the derived class as a template parameter to the base class is one of the easier exercises that is known under the name "Curiously Recurring Template Pattern" or CRTP for short. More advanced techniques require expression templates or even template metaprogramming.
I have not yet bought a copy of NR3, so I don't know if the above issues are addressed somewhere. If they are not, then I think it would be helpful to at least mention those techniques and recommend some resources for the interested reader.

Regards,

Yohm