Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: C++
Expertise: Beginner
Oct 18, 1999

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.

DevX Pro
 
Comment and Contribute

 

 

 

 

 


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

 

 

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