How to initialize a const char[] member variable

Discussion in 'C++' started by Henryk, Dec 5, 2005.

  1. Henryk

    Henryk Guest

    I'm using the gcc with the -pedantic setting.

    On my embedded system I need a parameter class that contains all
    setting for the software. This class needs to be ROM-able to locate it
    in some kind of eprom memory.

    Thus, all members of this class are const and there is only one static
    instance.


    An example header:

    class MyParameters
    {
    public:
    MyParameters() : m_nID(1), m_chDeviceName("The_Name") {};

    const unsigned char m_nID;
    const char m_chDeviceName[9];
    };

    I get the error:

    ISO C++ forbids assignment of arrays .... in the header file.

    How can I solve this? The only way I found is to make the class members
    static const. Then I can initialize them in the source file:

    const char MyParameters::m_chDeviceName[] = "The_Name";

    Thank you for your answers

    Henryk
     
    Henryk, Dec 5, 2005
    #1
    1. Advertising

  2. Henryk

    Jordan Guest

    Instead of doing an array, consider just using "const char*". You can
    iterate this character array using pointer arithmetic.

    char *cString = "Hello";
    // cString[1] == 'e'
     
    Jordan, Dec 5, 2005
    #2
    1. Advertising

  3. Henryk

    Old Wolf Guest

    Henryk wrote:

    > On my embedded system I need a parameter class that contains all
    > setting for the software. This class needs to be ROM-able to locate it
    > in some kind of eprom memory.
    >
    > class MyParameters
    > {
    > public:
    > MyParameters() : m_nID(1), m_chDeviceName("The_Name") {};
    >
    > const unsigned char m_nID;
    > const char m_chDeviceName[9];
    > };
    >
    > I get the error:
    >
    > ISO C++ forbids assignment of arrays
    > How can I solve this? The only way I found is to make the class
    > members static const.


    If all members of the class have the same device name, then
    read no further: the best thing to do is to make the device
    name static, as you have already explained.

    Assuming you want to allow different instances of the class
    to have different device names (which presumably will be
    passed to the class constructor):

    As the other poster pointed out, you could use a const pointer
    to const char. This won't use any extra memory, as you need
    the string in the string table anyway because it's a parameter
    to the constructor call in your above code.

    Another possible solution is:

    struct NineChars { char ch[9]; NineChars(char const *s); }

    struct MyParameters {
    const NineChars m_chDeviceName;
    MyParameters(char const *name): m_chDeviceName(name) {}
    };

    You could of course make this a template instead of
    hard-coding Nine.
     
    Old Wolf, Dec 5, 2005
    #3
  4. Henryk

    Henryk Guest

    Thank you both!

    Jordan, it works perfectly and if one thinks about it all makes
    sense... ;o)

    class MyParameters
    {
    public:
    MyParameters() : m_nID(1), m_chDeviceName("The_Name") {};

    const unsigned char m_nID;
    const char *m_chDeviceName;
    };

    Old Wolf, unfortunatelly I am not allowed to use templates and stuff.
    :eek:(

    But your first point is right. There is only one parameter set and even
    if there would be more the values would be the same. The static thing
    came up to my mind while I was posting my question yesterday. I think I
    should go for the static solution...
     
    Henryk, Dec 6, 2005
    #4
    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. Replies:
    24
    Views:
    836
    Netocrat
    Oct 30, 2005
  2. lovecreatesbeauty
    Replies:
    1
    Views:
    1,058
    Ian Collins
    May 9, 2006
  3. Javier
    Replies:
    2
    Views:
    565
    James Kanze
    Sep 4, 2007
  4. Replies:
    2
    Views:
    379
  5. fungus
    Replies:
    13
    Views:
    892
    fungus
    Oct 31, 2008
Loading...

Share This Page