Saturday, October 19, 2013

the bool type and the const qualifier

Below are a few points about bools and const.

Note that bool is an integer type but const is a qualifier, used when declaring other types - e.g. an int or a short - as a constant value.

Advantages of the const qualifier over the #define directive are that (1) you can define the type of the constant at the time, (2) you can use c++s scoping rules to limit scope if you want (3) you can use the const value to declare the size of an array.

bool is_ready = true; //normal use of bool

int ans = true; //ans assigned 1
int promise = false; //promise assigned 0 

bool start = -100; //start assigned true - any NON ZERO value is converted to true
bool stop = 0; //stop is assigned false - only ZERO is converted to false

const int MONTHS = 12; //MONTHS is symbolic constant for 12

const int toes; //value of toes is UNDEFINED!
toes = 10; //too late - you have messed up and cannot modify it now

No comments:

Post a Comment