Re: strings and NULL argument passing
On Nov 13, 9:00 pm, Jeff Schwab <j...@schwabcenter.com> wrote:
[...]
Make a version of print() which takes a const char* as
parameter and performs the check. If a non-null pointer is
passed, it simply calls the print() taking a std::string as
parameter, else it performs whatever error termination you
want (eg. assert()).
"assert" means "I know this is true." It's enforced
documentation, not a general-purpose way to terminate a
program.
Anyway, there's nothing here that implies termination. An
exception can be thrown, or the null pointer can just be
accepted as an empty string, or a C-style error code can be
returned. In my experience, the exception is usually the
right way to go.
A null pointer is not a string (in the general sense), nor is it
something which can be converted into a string. If his
interface requires a string, then passing it a null pointer
should cause an assertion failure. If his interface supports
the idea of a nullable string (e.g. something you might get when
reading a VARCHAR field from a database), then it has to support
something more than std::string.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34