This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

c++ structs and consts

Expert 2730 points


I have a problem with structs and const members:

struct foo
{
const int16 a;
const int16 n;
};

foo foobar = {1, 2};

With C this works without warnings, but with C++ I get the following warning (C2000 compiler TI v6.1.1):

warning #370-D: class "foo" defines no constructor to initialize the following:
const member "foo::a"
const member "foo::n"

Is there a way to get rid of this warning because this should be valid code, or?

  •  In C++ you need to have a constructor for such a struct.

    Replace 'foo foobar = {1, 2};' with foo foobar(1,2);

    Into the struct add:

     foo(_a,_n) : a(_a), n(_n) {}

     Of course, the simplest thing to do would be to remove const from the members, and just make the struct const.

    Thus,

    const foo foobar = {1,2};

    This would be correct in both languages.