alexhong2001 said:
When to return a const, from either a member function of a class or
non-member?
Call these "top level const":
void function1(int const q);
int const function2();
Don't do that. The 'const' has no meaning to the calling code, because both
values pass by copy. The qualification (const, non-const, volatile,
non-volatile) of the copy are irrelevant.
But do this:
class foo {
int bar;
public:
int const & getBar() { return bar; }
};
Do that so those who access foo::bar cannot change its value. The /Effective
C++/ books cover this.
Is assert() only executed in debug mode but skipped in release mode?
If your implementation's "release mode" defines NDEBUG, then assert() goes
away. But this newsgroup is not qualified to discuss "release mode", because
the configurations that your implementation provides by default are
implementation-specific. Questions about "release mode" will get the best
answers on your compiler's newsgroup.
I don't see much usage of "assert()." Am I missing its importance?
Assert() is the most important function in the whole programming industry.
You won't see it much in the tutorial code. But consider this simple
example:
int main()
{
Source aSource("a b\nc, d");
string
token = aSource.pullNextToken(); assert("a" == token);
token = aSource.pullNextToken(); assert("b" == token);
token = aSource.pullNextToken(); assert("c" == token);
token = aSource.pullNextToken(); assert("d" == token);
token = aSource.pullNextToken(); assert("" == token);
// EOT!
}
Passing the test requires objects of type Source to parse strings, ignoring
spaces and commas. If you add assertions to test cases like this, get them
to fail, and then edit your source to make the tests pass, you can grow
programs of any size and complexity, without the need to ever operate your
debugger. Run the tests after every 1~10 edits, and only perform the kinds
of edits that immediately return a program to a state where all the tests
still pass.
That technique trades long hours debugging for short minutes writing tests.