Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why a function with Bool argument accepts String?

Tags:

c++

c++11

I have a function which accepts an bool as an argument:

void func(bool a)
{
    doing something;
}

But when I'm calling the function and passed string to it:

func("false");

Actually it should accept only.

func(false);

It accepts string without any error.

why?

like image 891
waz Avatar asked Dec 06 '25 06:12

waz


1 Answers

Literal string is a char pointer, and pointer will be implicitly converted to bool.

  • Null pointer -> false
  • Non-null pointer -> true

In this case, the func will receive a true value from the non-null "false" string , which is const char* type.

You may need an interpreter function to able to read "false", "0", "no", etc from keyboard typing and convert to bool false which the program can understand.

like image 151
Chen OT Avatar answered Dec 07 '25 21:12

Chen OT