Advantages of bool type

Advantages of bool type

Standard C++ supports a built-in Boolean type: Bool. Variables of this type are can be assigned either a true or false values exclusively ( true and false are now reserved keywords in the laguage) and are at least as efficient as any other primitive type such as char or int. What are the advantages of using a bool instead of plain int?*Portability. All Standard conforming compilers support bool type. Should your code be ported, it is guaranteed that it will work as expected in other environments as well.*Readability. Needless to say, using explicit terms such as true, false and bool are much more self-documenting than the following deprecated example. In fact, such code samples are not guaranteed to work in future versions of C++ so they are better avoided:

 //better avoid this	int done = 0; //very bad; readers should guess 0 means


Share the Post: