initialize a class with array member data
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: initialize a class with array member data

  1. #1
    Join Date
    Nov 2003
    Posts
    34

    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

  2. #2
    Join Date
    Feb 2002
    Posts
    5,758
    One solution is a loop.

    Kuphryn

  3. #3
    Join Date
    Nov 2003
    Posts
    34
    Nice! A for loop in the constructor actually does yield an array full of zeros.

    Thanks, I never thought of that.

  4. #4
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    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)); }
    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds


    Article(s): Allocators (STL) Function Objects (STL)

  5. #5
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    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."

  6. #6
    Join Date
    Nov 2003
    Location
    Vienna, Austria
    Posts
    212
    ZeroMemory is just a macro that expands to memset, and it's Win-only besides unlike the ANSI C memset.
    All the buzzt
    CornedBee

  7. #7
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center