How do I initialize an array in a class file

This is a discussion on How do I initialize an array in a class file within the C++ Programming forums, part of the General Programming Boards category; Hi there, I have a question about array initialization in class files. What is the proper way to do it? ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485

    How do I initialize an array in a class file

    Hi there,

    I have a question about array initialization in class files. What is the proper way to do it? I have tried the following in a .h file.

    Code:
    const int arr [] = {123, 456, 789};
    Which gives an error complaining about brace init in the .h file. If I do it in the .cpp file then it won't get recognized in subclasses that are including the .h file. I want this array to be private to the class, as an internal data member.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,707
    Constant arrays are really a pain to work with in C++ classes. I have avoided them for this reason. You could carry a container instead, and assign something like so:
    Code:
    class foo
    {
      private:
      const vector<int> arr;
      public:
      foo(const int *const a, size_t size): arr(a, a + size) {}
    };
    It basically must be constructed like this, as any and all constants are handled in the initializer list. I used a vector here, but you could use any container.

    That's pretty much the only way without using the mutable keyword. The other way, calling an init function in the constructor body to set up the same array every time, depends on the array being mutable.
    Last edited by whiteflags; 10-08-2010 at 06:31 PM.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Quote Originally Posted by Subsonics View Post
    Hi there,

    I have a question about array initialization in class files. What is the proper way to do it? I have tried the following in a .h file.

    Code:
    const int arr [] = {123, 456, 789};
    Which gives an error complaining about brace init in the .h file. If I do it in the .cpp file then it won't get recognized in subclasses that are including the .h file. I want this array to be private to the class, as an internal data member.
    Code:
    //.h
    class Foo {
    	static const int arr[];
    };
    
    //.cpp
    const int Foo::arr[] = {123, 456, 789};
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Thanks for those suggestions. I ended up declaring it static which worked out fine in this case.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by whiteflags View Post
    Constant arrays are really a pain to work with in C++ classes. I have avoided them for this reason. You could carry a container instead, and assign something like so:
    Code:
    class foo
    {
      private:
      const vector<int> arr;
      public:
      foo(const int *const a, size_t size): arr(a, a + size) {}
    };
    It basically must be constructed like this, as any and all constants are handled in the initializer list. I used a vector here, but you could use any container.

    That's pretty much the only way without using the mutable keyword. The other way, calling an init function in the constructor body to set up the same array every time, depends on the array being mutable.
    If this is for statically allocated arrays then you could improve on your foo constructor somewhat by removing the need to pass a size parameter, and have the compiler automatically detect it (Also stops a hapless user of the class passing in a pointer or 'null' value rather than an array)
    Code:
    class foo
    {
    private:
      const vector<int> arr;
    public:
      template<int size>
      foo(const int (&a)[size]) 
          : arr(a, a + size) {}
    };
    
    int main()
    {
        int bar[5];
        foo my_foo(bar);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. how to initialize a static pointer array in a class?
    By nadamson6 in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2005, 10:47 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21