On 2006-06-10 13:05, Priya Mishra wrote:
Hi Everybody
Well I saw one open source code, Which is written in C++ in this code
i came across
very unusall style of coding I am bit fixed on this, please any one
from you all help me.
I am pasting the only refrence code.
MNode *MTree::FindNode(char *nodeName, MNode *startNode)
{
MNode *current=startNode?startNode:root; -----> A
I am not able to understand the why the initialisation of the current
is done in
the above fashion. Both MNode and MTree are the Classes.
where MNode and MTree are the classes, well in the MTree class there is
class MTree
{
public:
MNode *root;
MNode *FindNode(char *nodeName, MNode *startNode = 0);
I'm not 100% sure I understand your question but I'm making a guess here
that you are talking about why the initialization-line contains the
?:-operator.
The code that initializes current first checks if startNode is a null-
pointer, in which case it sets current to root and startNode if it's not
a null-pointer. I've rewritten the code slightly to make it more readabe:
MNode *current = (startNode ? startNode : root);
This combined with the fact that FindNode has a default-value for the
startNode-parameter makes it easy to search for nodes in the same tree/
graph as root since you can just call FindNode("nodename"); and
startNode will then be a null-pointer which will initialize current to root.
Erik Wikström
--
"I have always wished for my computer to be as easy to use as my
telephone; my wish has come true because I can no longer figure
out how to use my telephone" -- Bjarne Stroustrup