-
December 27th, 2003, 08:44 PM
#1
initialize a class with array member data
Given the below snippet from a class:
//////////////////////////////////////////////////
class safearray
{
private:
static const int LIMIT = 10;
int array[LIMIT];
public:
safearray()
{ }
//////////////////////////////////////////////////
Is there any way to use a constructor to setup array[] so that the item at index n is zero (or any other int)? I know how to set it up for other types - ie:
classname() : int(0)
{ }
or
classname(int a) : int(a)
{ }
but I'm not having any luck doing this for arrays.
Thanks for any help.
Matt
-
December 27th, 2003, 10:04 PM
#2
One solution is a loop.
Kuphryn
-
December 28th, 2003, 12:41 AM
#3
Nice! A for loop in the constructor actually does yield an array full of zeros.
Thanks, I never thought of that.
-
December 28th, 2003, 01:40 AM
#4
Originally posted by mojoamonkey
Nice! A for loop in the constructor actually does yield an array full of zeros.
Thanks, I never thought of that.
Much easier...
Code:
classname() { memset(array, 0, sizeof(array)); }
-
December 28th, 2003, 02:52 AM
#5
Or how about
ZeroMemory(...),
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
-
December 28th, 2003, 07:17 AM
#6
ZeroMemory is just a macro that expands to memset, and it's Win-only besides unlike the ANSI C memset.
All the buzzt
CornedBee
-
December 28th, 2003, 02:06 PM
#7
Originally posted by CornedBee
ZeroMemory is just a macro that expands to memset, and it's Win-only besides unlike the ANSI C memset.
Yes, I know. Just wanted to point out the macro as an option.
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
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
|