devxlogo

Declaring Data for an Object

Declaring Data for an Object

Question:
I’m trying to declare some data for an object such that, when necessary, other functions can use the object not only to perform member functions but also serve as a ‘data container’.

For example, I’m declaring a Date object that, among other things, contains an array of all the month ends. So in the class declaration I’d like to do this:

class Date {public:	Date();	~Date(){};	int monthEnds[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};};

And from then on be able to rely the Date object to say:

February = Date::monthEnds[2];

I am going insane trying to find a good referrence for declaring array data within a class.

Answer:
You cannot initialize data memebers inside the class body. You need to perform the initialization inside the constructor.

See also  Redefining Energy Management: Advanced Solutions for Modern Buildings
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