Re: Returning Nulls in Templates
* Ruben Safir, on 20.03.2011 06:37:
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. Let me copy and past a copy of the
entire program, since it is complex for me, but likely to be run of the
mill for the advanced C++ programmers here.
This is the main header file which was originally broken into a header
and a .cpp file but has since been combined to make the templates happy
================================================================================
#ifndef LINKLIST_H
#define LINKLIST_H
#endif
This '#endif' is too early; it should be at the very end of the file.
#include<iostream>
namespace CHAINLIST{
Reserve all uppercase names for macros.
Don't shout.
template<class unk>
class NODE{
Reserve all uppercase names for macros.
Don't shout.
public:
NODE<unk>( unk value, NODE<unk> *item_to_link_to = 0);
There's no need to repeat the template type parameter here.
inline unk value();
It's not necessary to designate the method as 'inline', since it's in a template.
On the other hand you should have a 'const' at the end there.
inline unk& value_ref();
inline void value(unk);
When you have exposed a member in all ways possible, and that's roughly what you
have done here, what's the point of having that member non-'private'?
In this case, nothing.
Remove the accessor and modifier stuff. Just make that value 'public'.
inline NODE<unk> * next();
Ditto, should be 'const'.
inline NODE<unk>& next_ref();
inline void next(NODE<unk> *);
Ditto, just make that next-pointer 'public'.
NODE<unk> * err_node;
unk err_value;
Uh huh, what's this? Two public data members that apparently serve no purpose.
In every node instance.
Think about it.
~NODE<unk>();
You don't need that non-virtual destructor.
Either make it virtual (to support class derivation), or remove it.
private:
unk _value;
NODE<unk> *_next;
When underscores are used to designate data members, the usual convention is to
put the underscore at the *end* of the name.
Underscore at front conflicts with the C conventions for linkage names and
internal names.
};
[snip]
template<class unk>
inline unk NODE<unk>::value(){
if(this)
return _value;
else{
return 0;
}
}
'this' is never zero.
[snipalot]
Cheers & hth.
- Alf
--
blog at <url: http://alfps.wordpress.com>