0

i'm trying to understand a piece of algorithm taken from a c++ code , here's the C++ code

inline void DBGame::make_chain( Edge* &newedge, Edge *edges[], Node *node ){ newedge->length = edges[0]->length + edges[1]->length;

for (int j = 0; j < 2; j++) {

edges[j]->parent = newedge; edges[j]->remove();

  // replace the old edge with the new edge at the other end
  // -------------------------------------------------------
  int k = (edges[j]->node[0] == node);
  newedge->node[j] = edges[j]->node[k];
  newedge->pself[j] = edges[j]->pself[k];
  *newedge->pself[j] = newedge;
  }

}

fyi, node is an object and not an integer.
I don't understand how to assign an integer value to int k by giving an true-false expression.

3 Answers 3

2

There's an implicit cast of bool to 0 or 1. It doesn't greatly add to readability or efficiency, and is at least partly a result of C not having a bool type.

This line:

int k = (edges[j]->node[0] == node);

is equivalent to this:

int k = edges[j]->node[0] == node ? 1 : 0;

And juut to add to this, as the original questioner seems to have decided to use the comments to extend the question a lot, there are also implicit casts from numeric types and pointers to bool.

So

if (node) 

is equivalent to

if (node != 0)

which is another shortcut I'm not exactly enamoured of.

Sign up to request clarification or add additional context in comments.

5 Comments

ok, got it , thanks sorry, I have another question , here's the code if (node->edge[j] && node->edge[j] != pedge) or Node *node = edge->node[0]; if (node == edge->node[1]) { if (node) { node->degree = 2; do you mind explain the mean of 'if (node->edge[j] ` or ' if (node)` , how' the rule an mean of assigning an object to an if statement , instead of assign a true-false expression
sorry? I need some english in that statement to make sense of it
what I mean is usually we give a true-false expression to an if statement,like if (A==B). but in the code the there's no boolean expression, it use an object as an expression , if(node) , node is an object not an expression
how does assigning an object to an if statement works in c++?
Node is a pointer, so if (node) will evaluate to false if the pointer is null
0

false is 0, true is everything except 0

Comments

0

The type of the result of edges[j]->node[0] == node is bool. It can be implicitly converted to int, false becoming 0 and true becoming a non-zero value, usually, 1.

5 Comments

ok, thanks.. got it .sorry .. I have another question, how does assigning an object to an if statement works in c++? example if(node[a][b]) {...} what I know, usually Iwe assign a true-false expression to an if instead of an object
Here it works other way around. An expression is implicitly converted to bool, equivalent to if(node[a][b] != 0)
nice , thanks, your explaination really healpful for a newbie like me :)
@Andrey I think there is a minimum reputation score required to up-vote, unfortunately.
@juanchopanza: does that apply to answers for his own question?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.