2

In my answer to this question it was mentioned that the code I posted wouldn't work if the variable was declared as const.

What if i want to initialize a long array that's const type

int main (){
    const int ar[100];
 //this is invalid in c++
 //something like this

    for (int i=0;i <100;++i)
        ar[i]=i;
    return 0;
}

Are there other ways to do this than using the #define method proposed in that question?

Community
  • 1
  • 1
Sagar Kar
  • 177
  • 1
  • 2
  • 10
  • Can you elaborate? I don't understand what you're asking – Cory Kramer Jun 10 '15 at 13:46
  • @CoryKramer I think he's saying that he can't modify a const int[] after definition, therefore he can't initialize it. – KABoissonneault Jun 10 '15 at 13:47
  • 1
    `const int ar[100];` is not even legal C++, you cannot leave the array un-initialized – vsoftco Jun 10 '15 at 13:50
  • 1
    I suppose this is a follow-up question to my comment to [this answer](http://stackoverflow.com/a/30756798/584518)? In that case, simply check the answer posted by yours sincerely to that question. – Lundin Jun 10 '15 at 13:57
  • After declaring a const variable, you can no longer change its values. You should assign the values when you instantiate the object or variable. – Ediac Jun 10 '15 at 13:58
  • @Lundin exactly, but i dont find proper ans there.... if u dont mind kindly guide me – Sagar Kar Jun 10 '15 at 14:28
  • @SagarKar What part of [my answer](http://stackoverflow.com/a/30757138/584518) didn't you understand? It is universal and applies to any data type, not just chars. – Lundin Jun 10 '15 at 14:30
  • @Lundin ow u said about that #define method.... i mentioned that i am looking for something other than that... thats a cool method indeed – Sagar Kar Jun 10 '15 at 14:33
  • 1
    There is no standard way to do so. [Here is a very similar question](http://stackoverflow.com/questions/21528288/c-structure-array-initializing) with a very similar answer by me, plus some answers suggesting non-standard gcc extensions. – Lundin Jun 10 '15 at 14:35
  • Thanks man! Nxt time i will try to post more meaningful question that dont go for downvote! @Lundin – Sagar Kar Jun 10 '15 at 14:39
  • This isn't a bad question, it is just very hard to understand for others what you mean, since you are referring to that other question, without posting a link to it. So nobody except me understood what you were asking :) – Lundin Jun 10 '15 at 14:46
  • @Lundin will takecar form next time :) – Sagar Kar Jun 10 '15 at 14:47
  • I took the liberty to edit your question and post links. – Lundin Jun 10 '15 at 14:49
  • @Lundin kudos to u.... bad deeds got me good teacher! ;) – Sagar Kar Jun 10 '15 at 14:51
  • you could also use templates: http://stackoverflow.com/questions/6060103/create-static-array-with-variadic-templates – m.s. Jun 10 '15 at 15:18
  • See [xxd](http://unixhelp.ed.ac.uk/CGI/man-cgi?xxd) – pmg Jun 10 '15 at 15:18

2 Answers2

2

There's no way to do it directly, without hacks.

In C++ one way to work around this problem is to take advantage of the fact that inside their constructors const objects are still modifiable. So, you can wrap your array into a class and initialize it in the constructor

struct Arr 
{
  int a[100];

  A() 
  {
    for (unsigned i = 0; i < 100; ++i)
      a[i] = i;
  }
};

and then just declare

const Arr arr;

Of course, now you will have to access it as arr.a[i], unless you override the operators in Arr.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
1

You could use a trick... initialize a regular array and then get a reference to a const array:

int arr[100];
for (int i=0; i<100; i++) arr[i] = i*i;

const int (&carr)[100] = arr;
6502
  • 112,025
  • 15
  • 165
  • 265