Re: Making this code generic with C++ Standard Lib
Jack wrote:
"Ulrich Eckhardt" <eckhardt@satorlaser.com>
???????:fd8tp4-och.ln1@satorlaser.homedns.org...
Jack wrote:
vector<D3DXFRAME_EX>& Find (std::string& FrameName)
{
[...]
return NULL;
}
'NULL' is not a vector<>.
Uli
Dear Uli,
Thanks for your prompt reply. I wonder how to get around this?
Typically, you would return some kind of pointer to the found element. This
can be a C++ pointer (i.e. the memory address of the found object) or an
iterator pointing into the container.
In any case, it would be really helpful if you documented what the function
is supposed to do. This might be clear to you, but not to others. In fact
sometimes when you try to write down what is supposed to do you will find
that you yourself didn't quite know exactly what you wanted, so it might
get you further in your development, too.
Also, please remove everything that is not necessary from your example code,
like the whole D3D stuff. If you can write a find function for an 'int' you
can do the same for whatever other datatype you want, but again it makes it
easier for you and other readers.
Note that another alternative is that you return the found element by
reference and throw an exception when the element is not found. I wouldn't
call this method 'find' though, because find typically implies that it can
fail.
Uli