1

I'm learning C++ with previous Java experience.I found the following example in the cplusplus.com :

int CDummy::isitme (CDummy& param)
{
  if (&param == this)
  { 
       return true; //ampersand sign on left side??
  }
  else 
  {    
       return false;
  }
}

My question is : Why does it return boolean false when I clearly declared the method as int ? It's never possible in Java. The link of the example is : here.

  • Are you asking why it's legal (that's because there's an implicit conversion from `bool` to `int`), or are you asking why the code is written that way (that can only be answered by the author)? Can you link to the source? As a matter of style, and with insufficient context to be sure, it should **probably** be defined to return `bool`. – Keith Thompson Jul 19 '16 at 22:59
  • 5
    @KeithThompson - The author is writing crap code IMHO – Ed Heal Jul 19 '16 at 23:00
  • I'm asking why it's legal.Is it even possible?As as person from Java, I see it as wrong. –  Jul 19 '16 at 23:00
  • 6
    I wouldn't trust an example that takes so long to say `return &param == this;`. – juanchopanza Jul 19 '16 at 23:00
  • http://www.cplusplus.com/doc/oldtutorial/classes2/ –  Jul 19 '16 at 23:01
  • Please add that link to the question. – Keith Thompson Jul 19 '16 at 23:01
  • 1
    Perhaps historical reasons? Very early versions of C++, and versions of C prior to C99, didn't have a `bool` type, so `int` was commonly used to represent boolean values (`0` for false, anything else (especially `1`) for true). That's no excuse for writing code like that for a C++ reference site. – Keith Thompson Jul 19 '16 at 23:03
  • @SittingBull cplusplus.com is a well known source for notoriously bad samples and wrong reference statements. I'd urgently recommend you switch to en.cppreference.com when looking for reference. – πάντα ῥεῖ Jul 19 '16 at 23:05
  • I've submitted a comment on that example. (Of course that won't help the other examples on the site, and I don't have time to do more.) – Keith Thompson Jul 19 '16 at 23:07
  • @πάνταῥεῖ Now I'm really afraid.I have been learning from this site. –  Jul 19 '16 at 23:10
  • 1
    @SittingBull I can feel with you. There are much better and reliable resources available than cplusplus.com actually. – πάντα ῥεῖ Jul 19 '16 at 23:12
  • 1
    @SittingBull You might be interested in reading [this post](http://stackoverflow.com/questions/6520052/whats-wrong-with-cplusplus-com). – πάντα ῥεῖ Jul 19 '16 at 23:16

2 Answers2

8

While the question of why the function does what it does is best answered by the author of the function, it is easy to explain why C++ allows such function to compile without a problem.

In C++ bool is a fundamental integral type, so it can be freely converted to a number: true becomes 1, and false becomes zero.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

This is just an instance of implicit conversion. In c++, 1 and true (also 0 and false) are the same thing(s).

You could also do something like

while (1)
   //Infinite loop

or

bool b = false;
if (b == 0) {
    //This is reached
}
else {
    //This is not
}
Cadoiz
  • 1,446
  • 21
  • 31
  • (Java is a bit less messy here, but the second example would afaik also work.) – Cadoiz Jul 20 '16 at 02:39
  • It is even the case [that all numbers other than 0 are truthy](https://www.cprogramming.com/reference/truth.html) – Cadoiz Feb 23 '23 at 07:39