D
DaveLessnau
In a book on Data Structures that I'm reading, the authors are
describing various linked lists and trees. In these, they start with
some form of node class. What's driving me crazy is that they define
these such that the data in those node classes is public. As a
specific example (a node they're going to use to build linked lists):
template <typename T>
class node
{
public:
T nodeValue; // data held by the node
node<T> *next; // next node in the list
node() : next(NULL) // default constructor w no init value
{}
node (const T& item, node<T> *nextNode = NULL) :
nodeValue(item), next(nextNode)
{}
};
They then use this node class (with its public data) in the private
areas of their linked list classes. They justify this by saying:
"Making the data in the node objects public does not violate the
object-design principle of information hiding, because end users do not
have access to these low-level objects."
In my ignorance, my feeling is that this is bunk. THEY have access to
those public data items and can mess things up themselves. Plus, if
they change the implementation of node, they'll have to change the code
wherever they use it (standard software engineering stuff). Wouldn't
it be better to make the data (nodeValue and *next) of that node class
private and then provide public setNode and getNode methods? I suppose
there might be some additional overhead in making the setNode and
getNode calls vs just changing the data directly. But, without
measuring the choke points in the completed application, there's no
telling if it makes a difference.
I'm really hoping I'm wrong about this, since it really frosts my
shorts that a couple of Computer Science professors wrote a book with
this kind of programming throughout it.
describing various linked lists and trees. In these, they start with
some form of node class. What's driving me crazy is that they define
these such that the data in those node classes is public. As a
specific example (a node they're going to use to build linked lists):
template <typename T>
class node
{
public:
T nodeValue; // data held by the node
node<T> *next; // next node in the list
node() : next(NULL) // default constructor w no init value
{}
node (const T& item, node<T> *nextNode = NULL) :
nodeValue(item), next(nextNode)
{}
};
They then use this node class (with its public data) in the private
areas of their linked list classes. They justify this by saying:
"Making the data in the node objects public does not violate the
object-design principle of information hiding, because end users do not
have access to these low-level objects."
In my ignorance, my feeling is that this is bunk. THEY have access to
those public data items and can mess things up themselves. Plus, if
they change the implementation of node, they'll have to change the code
wherever they use it (standard software engineering stuff). Wouldn't
it be better to make the data (nodeValue and *next) of that node class
private and then provide public setNode and getNode methods? I suppose
there might be some additional overhead in making the setNode and
getNode calls vs just changing the data directly. But, without
measuring the choke points in the completed application, there's no
telling if it makes a difference.
I'm really hoping I'm wrong about this, since it really frosts my
shorts that a couple of Computer Science professors wrote a book with
this kind of programming throughout it.