Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Home » Tip Bank » C++
Language: C++
Expertise: All
Oct 9, 1998



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Increment and Decrement Operators Should not be Applied to Bool Variables

According to the C++ Standard, you are still allowed to apply operator ++ to a variable of type bool for backward compatibility with old code. However, this practice is deprecated and most likely will not be supported in the future. Therefore, you should not use it in new code. Incrementing a bool always yields a 'true' value, regardless of the original value:
	bool isConnected = true;
	bool isTransmitting = false;
	SOCKET socket = browser.Connect(); 
	if (socket) 
  isConnected++;   // value remains 'true'
  isTransmitting++;   // 'true' value assigned
Keep in mind that applying -- (decrement operator) to a bool is illegal.
Danny Kalev
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date