Search code examples
c++functionreturn-value

Why the function returns boolean when it's declared int?


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.


Solution

  • 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.