[Stk] noob pointer question about the tutorials

Richard Dobson richarddobson at blueyonder.co.uk
Wed, 14 Nov 2007 10:07:29 +0000


malik martin wrote:
> why in "hello Sine!' was FileWvOut duplicated as a class instance 
> named "output" and in realtime auido blocking, an instance was
> created as a pointer, "*dac"? i read that the class is protected, and
> i've read about pointers and classes a bit. not that i can truly say
> i remember it but, why so much protection? why not make an instance? 
> will this instance affect the base class or something?
> 
> oh and what's this address operator doing?? "( StkError & ) " the 
> address of what? it's by itself. and for that matter i never really
> understude the return codes 0, 1, etc. i was just taught to know
> that, typically, return 0; exits the program. the reason i as is
> cause of  " exit(0);"
> 


If the constructor of a class can throw an exception (which wvOut can)
it is best (necessary even) to create it via new inside a try block, so
the exception can be caught.  Where (as in most cases) the constructor
does not throw an exception (the default constructor of fileWvOut will
always complete), you can declare an instance on the stack, as "hello
Sine" does. It is a matter for debate whether a constructor that can
throw an exception (especially the one and only default constructor) is
a good thing or not.

You need to read up on exceptions in C++ - much of it will
not make sense until that aspect is understood.


The notation "StkError &" signifies a C++ reference variable; which you
also need to read up on in great detail (used for all sort of things,
but very much in exception handling). It is a ~sort of~ pointer that is
~guaranteed~ to point to an valid object (enforced by the language);
whereas a normal C pointer can be declared but not initialised, so could
be NULL or point to rubbish memory. You ~never~ have to test a reference
argument or variable to see if it is valid or not. It is scarcely 
possible to write or read non-trivial C++ without knowing about references.

The return value of a program is often irrelevant, but it can be read by
the parent process (which might be a unix shell script, among other
things), which can read the return value to decide the next action.  By
convention, a return value of 0 signifies success, while any other value
(positive or negative)  signifies some error.  Thus at the end of a
program, it is usual to write "return 0" to signify successful
completion of the process.

"return" has no special meaning in terms of exiting a program; you use
it in any function that is defined to return a value. main() itself is
defined by C and C++ to return an int, so you use return at top level to
terminate the program. "exit" is a shortcut (some would say a lazy one),
to enable a program to terminate from some arbitrary point deep within it.

It has to be said that wrriting exit(0) inside an exception handler is
not really cricket (speaking as an Englishman!) - it should be exit(1)
or something, to communicate the error to the outside world (the
argument to exit() becomes the return value of the program). But of
course programmers are notorious for bending such conventions to their
personal will!




Richard Dobson