How to initialize a const char[] member variable

H

Henryk

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
 
J

Jordan

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'
 
O

Old Wolf

Henryk said:
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.
 
H

Henryk

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...
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top