CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2012
    Posts
    53

    initilaize static const array

    hi,

    this is what i would like to do:
    PHP Code:
    //test.h
    class test
    {
    static const 
    int test::arr[5] = {1,2,3,4,5};

    but i am not allowed to...
    Instead i have to initialize the array separately

    PHP Code:
    //test.h
    class test
    {
    static const 
    int arr[5];
    }

    //test.cpp
    const int test::arr[5] = {1,2,3,4,5}; 

    Out of interest, why?
    The first version seem a lot more intuitive to me; and all the code & data is nicely in one place.
    Does the actual way yield any advantage i dont see right away?

    just wondering...
    Last edited by tuli; June 17th, 2012 at 02:08 PM. Reason: fixed code

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: initilaize static const array

    Out of interest, why?
    You are not allowed to initialize values inside the class definition. As said, it's the definition of the class... nothing more, nothing less.

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,062

    Re: initilaize static const array

    Well, there is an exception for static const integer member variables, but an array thereof, nope.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



HTML5 Development Center

Click Here to Expand Forum to Full Width