Const array

Discussion in 'Programmer's Corner' started by mentaaal, Feb 28, 2009.

  1. mentaaal

    Thread Starter Senior Member

    Oct 17, 2005
    451
    0
    Hi guys,

    quick question, just wandering is it possible to define a const array of anything as a data member of a class?

    Below is an example of what I mean (THis code does not compile by the way!)

    Code ( (Unknown Language)):
    1. #include <iostream>
    2. using namespace std;
    3.  
    4. class test
    5. {
    6. public:
    7.     test();
    8.     void output();
    9. private:
    10.     const char A[2] = {'h','d'};
    11. };
    12.  
    13. test::test()
    14. {
    15.     //do nothing
    16. }
    17. void test::output()
    18. {
    19.     cout << A[0];
    20.     cout << A[1];
    21. }
    22.  
    23. int main()
    24. {
    25.     test test1;
    26.     test1.output();
    27. }
    28.  
     
  2. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    const arrays in C++ are pretty dim in my memory, and I wasn't able to find out anything about them in 3 or 4 references. It's possible that they have to be initialized in the class constructor. Or make the array static.
     
  3. mentaaal

    Thread Starter Senior Member

    Oct 17, 2005
    451
    0
    Cool thanks, i'll give it a bash
     
Loading...