devxlogo

How to define an object-internal constant?

How to define an object-internal constant?

When you need a constant integer member in an object, the easiest way to create one is by using a nameless enum, which can be used like any

  const int: class vector {	int v_size;	enum {MAX = 1024}; //nameless enum 	char *p;	public:	vector() {p = new char[MAX]; }	vector( int size) {if (size <= MAX)				 	p = new char[size] ;		      	else 					p = new char[MAX]; }}
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist