How to initialise const static char array in a header only class?

Discussion in 'C++' started by Joost Kraaijeveld, Sep 29, 2012.

  1. Hi,

    I want to initialise a const static char array in a class that should be
    header only. The header will be included by many clients within the same
    program. Ordinary int and char variables I can initialise in the class
    declaration. But an array fails, e.g.

    class Message
    {
    public:
    static const char majorVersion = 1; // OK
    static const char minorVersion = 1; // OK
    static const char magicNumber[] = {'1','0','0','1'}; // Not OK
    }

    results in the following compiler (GCC) error:

    error: in-class initialization of static data member ‘const char
    Message::magicNumber []’ of incomplete type.

    Is this a matter of mere syntax or is this something that cannot be
    done? Or is there another way to get the same effect, without resorting
    to a cpp file?

    TIA

    Joost
     
    Joost Kraaijeveld, Sep 29, 2012
    #1
    1. Advertising

  2. On 9/29/2012 11:10 AM, Joost Kraaijeveld wrote:
    > I want to initialise a const static char array in a class that should be
    > header only. The header will be included by many clients within the same
    > program. Ordinary int and char variables I can initialise in the class
    > declaration. But an array fails, e.g.
    >
    > class Message
    > {
    > public:
    > static const char majorVersion = 1; // OK
    > static const char minorVersion = 1; // OK
    > static const char magicNumber[] = {'1','0','0','1'}; // Not OK
    > }


    ;

    >
    > results in the following compiler (GCC) error:
    >
    > error: in-class initialization of static data member ‘const char
    > Message::magicNumber []’ of incomplete type.
    >
    > Is this a matter of mere syntax or is this something that cannot be
    > done?


    The Standard requires the static constant member to be of integral or
    enumeration type if you want to initialize it in the class definition
    using the brace-or-equal-initializer ([class.static.data]/3).

    > Or is there another way to get the same effect, without resorting
    > to a cpp file?


    Nope.

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Sep 29, 2012
    #2
    1. Advertising

  3. Joost Kraaijeveld

    SG Guest

    On Saturday, September 29, 2012 5:10:58 PM UTC+2, Joost Kraaijeveld wrote:
    > Hi, I want to initialise a const static char array in a class that
    > should be header only. The header will be included by many clients
    > within the same program. [...]
    > Is this a matter of mere syntax or is this something that cannot
    > be done?


    It's not a matter of syntax. Static data members are NEVER defined in a class definition, only declared. As a special exception, the compiler lets you "initialize" _constant_ _integral_ (or scalars in general?) members.

    > Or is there another way to get the same effect, without resorting to
    > a cpp file?


    Do you have a good reason for defining an array inside a header? Not requiring a CPP file is not a good reason, IMHO.

    But you can do things like

    typedef int five_ints[5];

    inline const five_ints& foo()
    {
    static const five_ints arr = {1,2,3,5,8};
    return arr;
    }

    in a header file -- or even write

    template<class Dummy=void>
    class MessageBase
    {
    public:
    static const int arr[5];
    };

    template<class Dummy>
    const int MessageBase<Dummy>::arr[5] = {1,2,3,5,8};

    class Message : public MessageBase<>
    {
    };

    since multiple definitions of inline functions and members of class templates are allowed within a program. But the purpose of these rules is to support templates and to ease inlining. It's not to allow users to define arrays in header files.

    Cheers!
    SG
     
    SG, Oct 1, 2012
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Santa
    Replies:
    1
    Views:
    1,140
    Mark A. Odell
    Jul 17, 2003
  2. Replies:
    24
    Views:
    889
    Netocrat
    Oct 30, 2005
  3. lovecreatesbeauty
    Replies:
    1
    Views:
    1,130
    Ian Collins
    May 9, 2006
  4. Owen Ransen
    Replies:
    5
    Views:
    405
    Owen Ransen
    Apr 27, 2007
  5. Eric Sosman
    Replies:
    0
    Views:
    613
    Eric Sosman
    Mar 18, 2012
Loading...

Share This Page