-
December 27th, 2005, 10:19 AM
#1
initializing an array in a class object
I am trying to initialize an array (julian days by month) when an instance of a class object is created. I have no idea how to do this. have tried the standard approach of initialization as done in global or local variable declarations within the class:
class myclass
{
long int juliandays[11] = {31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
public
myclass();
}
and that gives me a compiler error. Prefixing the variable with static (since only one copy is really needed) gave the same error.
The only way I have been able to initialize that variable is by brute force entry of each element in the constructor function. There MUST be a better way ! (I'd hate to have to initialize an array of 100 elements in that manner.)
Does anyone know the correct syntax to accomplish this seemingly trivial task?
Thanks,
Brian
-
December 27th, 2005, 10:32 AM
#2
Re: initializing an array in a class object
You probably want 1) const and 2) static in this case.
See this article on how to initialize the variable properly:
http://www.devx.com/tips/Tip/5602
http://www.google.com/search?hs=uCG&...2B&btnG=Search
-
December 27th, 2005, 10:37 AM
#3
Re: initializing an array in a class object
When you make a variable static - you need to define it outside the class in one of the source code (.cpp) files. It is this, that you did not do and hence seeing an error. Try doing that if you really think only one copy of it is needed for all your objects. Also make it a const if you think it is not going to change. You can also try the option of enums if you can look for a name for all these static consts.
As for initializing arrays in using constructor initialization list and the limitations, have a look at this beautiful thread : Constructors and arrays. Hope this helps. Regards.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
December 27th, 2005, 10:59 AM
#4
Re: initializing an array in a class object
Hi!
Tha "static" approach was just a desparate attempt. I searched the site for initialization of arrays in a class object with various wordings on the search and could not find anything. My C++ book has nothing, either.
The thread you refer to describes initialization of arrays of objects but not of an array within an object.
One way is to define my constructor as
myclass(juliandays);
and then upon creation of the instance (maybe some syntax errors here)
myclass x(31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334);
but that's a worse choice because I want to bury the initialization in the class creation so I DON'T have to remember the long list of values.
Maybe there is no way to intialize an array within a class.
Brian
-
December 27th, 2005, 11:03 AM
#5
Re: initializing an array in a class object
Brian,
did you take a moment to look at the article I posted? It does static const initialization? It can be very easily carried over into arrays.
Here's an MSDN article showing as such:
http://msdn.microsoft.com/library/de...m/specl_32.asp
-
December 27th, 2005, 11:24 AM
#6
Re: initializing an array in a class object
Hi Eli,
Just took a look.
It seems that if I write
myclass
{
public:
static const long int juliandays[11];
myclass();
};
const long int myclass::juliandays[11] = {list of 11 values};
will do the trick.
I will give it a try!
Thanks (It was a lot harder than I thought it would be; wonder why you can't just do it in the variable declaration?)
Brian
-
December 28th, 2005, 02:30 AM
#7
Re: initializing an array in a class object
Originally Posted by Gyannea
The thread you refer to describes initialization of arrays of objects but not of an array within an object.
I am sure you did not have a good look at the thread I mentioned. Regards.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|