On Sunday 30 March 2003 04:57 pm, Jerry Feldman wrote: ...
Then to print the string, you may want to create a friend function so that you can do something like:
HelloClass hw("Hello, World!");
cout << hw << endl; This should be in one of your books.
This looks a lot like the MyClass.toString() of Java. I'm not sure I have the best books for learning C++. The Ellis and Stroustrup book seems to be about 50% apology for doing things Stroustrup would rather not have, but for the desire to cooperate with existing C program(mer)s. It's interesting, but certainly doesn't present the language as deftly as K&R does C. K&R is one very well written book. I have Horstmann's 1991 edition. It's probably dated. Pohl's book is likewise a decade old. I'm sure they are all worth reading, but they probably don't provide the best treatment of recent advances, and imporved libraries, etc. They will have to do for now, however.
Let the constructor assign the the string. That does not eliminate the desire for setting a message, but you can also use some overloading such that: hw = "New Value";
For the HelloClass, using a pointer is probably not altogether useful. But, you also need to be aware of some subtle performance issues. But, at this stage, try to concentrate on setting up constructors with optional arguments and default values. Without complexity, look at setting up things like copy constructors: HelloClass::HelloClass(HelloClass &s) { str = s.str; }
Assignment operator, and possibly concatenation. You wil be reinventing the wheel a bit, but you will also be learning. Don't worry too much about efficiency. Once you are experienced in the language, then you can be more efficient.
In any kind of computer programming, the most important issue is to accomplish the task at hand. But, I always try to make my code readable and maintainable. One reason is that I have had to maintain some horrible code (eg. the sources for YACC and LEX). The other reason is that I want those who come behind me not to curse me. In this industry there are always cases where when looking for a job, someone who knows you may be on the hiring side. But even for your own personal code, you might put it down and not look at it for a while.
I place readability very high on my list of priorities. Good structure is also very important. I've wasted a good deal of time debugging problems that arose because I tried to cut corners. Basically I believe in always writing code 'correctly', hense the question about undepricated Hello World. I could have simply turned off the deprication message and continued to march. I also belive in finding a reasonably good solution to a generic problem, and applying it consistently rather than inventing different 'creative solutions' to the same problem. C++ seems to be rife with opportunities to hang yourself with the available rope. One concern I have is to learn 'safe' coding in C++. I know there are all kinds of pitfalls that Java simply doesn't allow you to get near. Most of these have to do with pointers and memory management. I took a look at http://lxr.mozilla.org/seamonkey/source/security/nss/cmd/certcgi/certcgi.c and found myself amazed, horrified, amused and baffled. STH