0

How can I initialize and use an static constant string array? I made an example, the header:

const static string validFileTypesToSendToClient[];

The implementation:

const string Settings::validFileTypesToSendToClient[] = {"html","css","js"};

This works, but how can I use this? For example in this case:

string fileTypesToAllow[] = Settings::validFileTypesToSendToClient;

I get the error:

Initialization with ‘{…}’ expected for aggregate object.

So, how can I use a static constant array of string in a proper way? I already found this: Initializing a static const array of const strings in C++ but they don’t describe how to use it.

2
  • you need a c++ text book. that said, indexing in c++ is the same as in most other languages. raw arrays are non-assignable. Commented Dec 30, 2012 at 10:51
  • I forgot that C++ makes array's on the stack. My bad! Commented Dec 30, 2012 at 10:57

4 Answers 4

1

Built-in array assignment doesn't exist in C++. You can, however, do this:

#include <array>
#include <string>

std::array<std::string, 3> string_array = 
{
  "foo", 
  "bar", 
  "baz", 
};

int main()
{
  std::array<std::string, 3> second_string_array = string_array;
}

It's C++11, but it is supported by almost every compiler nowadays. (It seems questionable why you would like to copy such an array in the first place btw. Also, this is one of the cases were I'd consider using const char*, even though you can definitely forget the assignment with that one.)

Sign up to request clarification or add additional context in comments.

3 Comments

This was just an example, in my code I want to get the array size. But that doesn't work so well.
@lauw I don't really understand your question. If you want to get the size just use string_array.size(), or, for built-in arrays, sizeof string_array / sizeof *string_array.
Thanks, that worked. But I was just exploring the possibilities.
1

This line:

string fileTypesToAllow[] = Settings::validFileTypesToSendToClient;

is wrong. C/C++ doesn't allow array assignment. You may assign an array to pointer, but another array.

Comments

1

In the following line, you are declaring a new array of strings, and trying to initialize it with another array. This is not allowed in C++.

string fileTypesToAllow[] = Settings::validFileTypesToSendToClient;

What you should do instead is to declare the array, and then copy the values over:

const int size = 3; // Same as the size of the Settings::validFileTypesToSendToClient array
string fileTypesToAllow[size];

for(int i = 0; i < size; i++)
{
    fileTypesToAllow[i] = Settings::validFileTypesToSendToClient[i];
}

Comments

1

You can't assign an array to another array, you have to copy element one by one:

 string fileTypesToAllow[3];

 for(int i=0; i<3; i++)
 {
    fileTypesToAllow[i] = Settings::validFileTypesToSendToClient[i];
 }

To access array, you could do like this:

std::cout << Settings::validFileTypesToSendToClient[0] << std::endl;

Could just use vector instead of array or std::array

struct Settings
{
  const static std::vector<std::string> validFileTypesToSendToClient;
  static std::vector<std::string> makeData() 
  { 
    std::vector<std::string> v;
    v.push_back("html");
    v.push_back("css");
    v.push_back("js");
    return v;
  }
};

Implementation:

const std::vector<std::string> Settings::validFileTypesToSendToClient = Settings::makeData();

Now you can copy validFileTypesToSendToClient easily:

int main()
{      
  std::vector<std::string> fileTypesToAllow = validFileTypesToSendToClient;
  return 0;
}

5 Comments

As far as I know initialization with {} is not allowed for vectors. I give it a try.
That gives me this error: "non-aggregates cannot be initialized with initializer list"
Why isn’t it possible to mark 2 questions as the answer :(.
haha, it only allows one as the best answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.