Re: References: I need a quick bit of .
xkenneth wrote:
> All,
>
> Thanks to anyone who reads this. I am trying to implement a basic
> dictionary class using a binary tree. That information is fairly
> unimportant as I'm simply trying to use references to contain the node
> data. When i create my tree, create it's root node, and then try to
> return the data, it comes back as either garbled or the wrong data. I
> have attached all of my code, any is appreciated.
>
> binNode.h
> ----------------
>
> #ifndef BINNODE_H
> #define BINNODE_H
>
> class binNode
> { public:
> [...]
> float& data;
> };
>
> #endif
> -------------------
>
> binNode.cc
> -------------------
> #include <iostream>
> #include "binNode.h"
>
> binNode::binNode(binNode* left,float& item,binNode* right) : data(0) {
> [..]
That should NOT compile. You're initialising a _reference_ to non-const
'float' using a _literal_ of type 'int' (the zero). If your compiler
does not complain, throw it away and get a better compiler.
Once you do, rethink your decision to store references in your nodes.
References are very delicate things, they cannot be "re-seated" to refer
to a different object. Once initialised, they refer to the same object
as long as they live; assigning to a reference changes the object to
which the reference refers.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
|