473,388 Members | 1,207 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Doubt in Code

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);
Thanks and Regards
~Priya

Jun 10 '06 #1
2 1394
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
Jun 10 '06 #2
In article <1149937533.783298.246130
@h76g2000cwa.googlegroups.com>, mi*******@gmail.com
says...

[ ... ]
MNode *MTree::FindNode(char *nodeName, MNode *startNode)
{
MNode *current=startNode?startNode:root; -----> A


This allows you to tell it where to start the search
from, but if you pass a null pointer, it starts from the
root.

The declaration probably includes NULL as the default
value for the parameter. When you make a call, you just
pass the string you want to look for. It probably
operates recursively, and this avoids having a front-end
function that does nothing but pass the root to the
internal function that does the real searching.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 11 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Srini | last post by:
Hello all, I was going thru the GotW archives where I had a doubt in this particular item. http://www.gotw.ca/gotw/027.htm There is a mention about a subtle change to the standard in July...
77
by: muttaa | last post by:
Hello all, My doubt is going to be so primitive that i ask you all to forgive me beforehand.... Here's the code snippet: int main() { int x=5;
5
by: edu.mvk | last post by:
Hi Friends, I have subscribed to this group recently. I have a doubt in templates in C++ A fraction of code given in "The C++ Programming Language" -By B.Stroustrup is as follows : ...
11
by: Bob Nelson | last post by:
I don't remember seeing the term ``doubt'' used much in c.l.c. back in the 90's. When did this word become nearly synonymous with ``question'' or ``query'' and does it have static duration?
38
by: edu.mvk | last post by:
Hi I am using strcpy() in my code for copying a string to another string. i am using static char arrays. for the first time it is exected correctly but the second time the control reaches...
122
by: ivan | last post by:
hi all, if I have: if(A && B || C) which operation gets executed first? If I remeber well should be &&, am I correct? thanks
5
by: Paulo | last post by:
Hi, I have a RadioButtonList and I need to do some verifications on a "OnChange" event on client... because on classic asp/html I just add a "onChange" event on <input type="radio" onChange="">,...
2
by: rajendra | last post by:
hi friends, i have placed code because i want to explain my doubt clearly to you ,and i have placed my doubt in Main of the program (static void Main(string args)) by // before that ...
7
by: new to c | last post by:
Hi! I write the 2 codes int i; i = sizeof(long int); printf("%i", i); i = sizeof(int long); printf("%i", i);
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.