Automatic Counting

This is a discussion on Automatic Counting within the C++ Programming forums, part of the General Programming Boards category; Referring to - http://www.codeguru.com/cpp/tic/tic0113.shtml Code: //: C10:StaticArray.cpp // Initializing static arrays class Values { // static consts can be initialized ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    99

    Automatic Counting

    Referring to - http://www.codeguru.com/cpp/tic/tic0113.shtml

    Code:
    //: C10:StaticArray.cpp
    // Initializing static arrays
    
    class Values {
      // static consts can be initialized in-place:
      static const int scSize = 100;
      // Automatic counting works with static consts:
      static const float scTable[] = {
        1.1, 2.2, 3.3, 4.4
      };
      static const char scLetters[] = {
        'a', 'b', 'c', 'd', 'e',
        'f', 'g', 'h', 'i', 'j'
      };
      // Non-const statics must be 
      // initialized externally:
      static int size;
      static float table[4];
      static char letters[10];
    };
    
    int Values::size = 100;
    
    float Values::table[4] = {
      1.1, 2.2, 3.3, 4.4
    };
    
    char Values::letters[10] = {
      'a', 'b', 'c', 'd', 'e',
      'f', 'g', 'h', 'i', 'j'
    };
    
    int main() { Values v; } ///:~
    Automatic counting works with static consts: - what is automatic counting?

    but you cannot use automatic counting for non- static const arrays - explaination?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    >> what is automatic counting?
    Notice that there is no size specified for that array. The compiler automatically counts the values in the initializer and uses that for the size.

    >> explaination
    The compiler must have enough knowledge about the class to create an object by the end of the class definition, including the exact sizes of all the components. So you have to provide a size inside the class declaration for a non-static array, even if it is const.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    99
    Code:
    #include<iostream>
    using namespace std;
    
    class Values 
    {
      static const int scSize = 100;
      static const float scTable[] = {1.1, 2.2, 3.3, 4.4 };
      static const char scLetters[] = {'a', 'b', 'c', 'd', 'e','f', 'g', 'h', 'i', 'j'};
    
      static int size;
      static float table[4];
      static char letters[10];
    };
    
    int Values::size = 100;
    
    float Values::table[4] = {1.1, 2.2, 3.3, 4.4};
    
    char Values::letters[10] = {'a', 'b', 'c', 'd', 'e','f', 'g', 'h', 'i', 'j'};
    
    int main() 
    { 
        Values v;
    }
    i tried to compile the above code, in the definition of static const arrays inside the class

    i get error - brace enclosed initializer is not allowed here

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    Post the full error, including which line it refers to.

    What compiler/IDE are you using?

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    99
    Quote Originally Posted by Daved View Post
    Post the full error, including which line it refers to.

    What compiler/IDE are you using?
    g++ test.cpp

    test.cpp:7: error: a brace-enclosed initializer is not allowed here before '{' token
    test.cpp:7: error: invalid in-class initialization of static data member of non-integral type `const float[]'
    test.cpp:8: error: a brace-enclosed initializer is not allowed here before '{' token
    test.cpp:8: error: invalid in-class initialization of static data member of non-integral type `const char[]'

    using GCC/Bloodshed

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    I'm not sure if the codeguru page is accurate according to the standard or not. To my knowledge only integral types could be initialized in the class like that. Perhaps they use a different compiler that allows it as an extension.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting Numbers in Array, not counting last number!
    By metaljester in forum C++ Programming
    Replies: 11
    Last Post: 10-18-2006, 11:25 AM
  2. counting and output, with arrays and functions
    By Wraithan in forum C++ Programming
    Replies: 7
    Last Post: 12-04-2005, 11:46 PM
  3. why automatic variables are assigned with garbage values
    By srinivasg in forum C Programming
    Replies: 1
    Last Post: 11-08-2005, 06:14 AM
  4. automatic variables
    By FOOTOO in forum C Programming
    Replies: 5
    Last Post: 03-08-2005, 05:30 PM
  5. Automatic vs. Static Duration
    By JMB in forum C++ Programming
    Replies: 7
    Last Post: 10-14-2002, 07:16 PM

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