Enums instead of const member variables

This tip submitted by Motiram Patil on 2013-10-08 08:24:03. It has been viewed 4580 times.
Rating of 5.1 with 27 votes



Use enum instead of const member variables.

Consider a class Heart as below:
class Heart
{
private:
	enum BitsPerSec {eHuman = 72};
public:
	int HumanBitsPerSecond() {return eHuman;}
};

This class can be written as:
class Heart
{
private:
	const int nHumanBitsPerSec = 72;
public:
	int HumanBitsPerSecond() {return nHumanBitsPerSec;}
};

Advantages:
First version of Heart will be of 4 bytes.
Second version of Heart will be of 1 byte.




More tips

Help your fellow programmers! Add a tip!