Re: Returning Nulls in Templates
On Mon, 21 Mar 2011 01:06:22 -0700, Goran wrote:
On Mar 20, 6:37??am, Ruben Safir <ru...@mrbrklyn.com> wrote:
I'm having a very difficult time returning NULLS in my program which
started out as an exercise of Linked Lists based on Lippman, but has
evolved as I test out new skills and methods. ??I've templated it an
then I added some hooks for MYSQLb but I've run into trouble with
returning NULL values of zero's, especially with the operator[] which
requires the return value being a reference.
operator[] can return whatever you want it to. It does not require a
reference.
Otherwise, your problem is that you are trying to return a value for a
case where you don't have it. It's a logic problem and has nothing to do
with 0, C++, std::string, or templates.
I agree with you. This is just the locations where I've failed to find a
logical solution for. If I'm not mistaken, C file handler types have
structs where one of the members are an error state. In Oracle Database
programming, it definitely has a struct that has an error state within
the cursor. That's why I started to add err and err_ref to my object
nodes, because your right, I'm trying to avoid Try::Catch at this time
and I don't want to just crash to the program.
What I learned here though, and which isn't readily apparent from reading
the texts, is that there is no true way to implement the behavior of a
native array.
In a native array:
int a[10]= {101,102,103,104,105,106,107,108,109};
int tmp;
tmp = a[3]; // passed by value
std::cout << tmp << end; // output 104
std::cout << a[3] << end; // output 104
tmp = 200;
std::cout << tmp << end; // output 200
std::cout << a[3] << end // output 104
a[3] = 500; //operator[]() is naively now in C++ equiv of pass by
//reference mode
std::cout << tmp << end; // output is STILL 200
std::cout << a[3] << end // output is not 500
Usually, when for a "no return value" case, operator[] is implemented in
two ways:
* proclaim undefined behavior (that is, crash) * throw an exception.
I'm coming to appreciate this more and more. I'm not that happy with it,
but that doesn't make me a better C++ programmer.
Returning "error" value, that you seem to want to do, is quite unusual.
I don't think anyone here would agree with this course of action (in
general, but context trumps all, and one could probably think of a
context where "err" would do better). Sure, there's no crash (good), and
there's no exception (perhaps good), but it requires the caller either
to check for err after each call, either or to account for err in
general. That's too complicated for such a simple thing.
I might agree, but a lot of standard programming includes checking for
errors and I was taught such defense coding when I learned C programming
and in a few other languages.
Eventually I'm going to rework this entire exercise based on what I
learned and much of the advice I got from the advanced coders who have
volunteered there comments and time.
Ruben
Goran.