c++ - Initialize a Struct containing a const array with initializer list -


i working c++11 , have class containing following struct:

struct settings{     const std::string name;      const std::string* a;     const size_t a; };  class x {     static const settings s;     //more stuff }; 

in .cpp file want define this

x::s = {"myname", {"one","two","three"}, 3}; 

but not work. work using intermediate variable

const std::string inter[] = {"one","two","three"}; x::s = {"myname", inter, 3}; 

is there way without intermediate variable?

a pointer cannot initialized list of values. use std::vector instead:

#include <vector>  struct settings{     const std::string name;     const std::vector<std::string> a; //        ^^^^^^^^^^^^^^^^^^^^^^^^     const size_t a; }; 

you can write:

class x {     static const settings s;     //more stuff };  const settings x::s = {"myname", {"one","two","three"}, 3}; 

here live example.

as suggested praetorian in comments, may want replace std::vector std::array, if acceptable specify size of container explicitly, , if size not need change @ run-time:

#include <array>  struct settings{     const std::string name;     const std::array<std::string, 3> a; //        ^^^^^^^^^^^^^^^^^^^^^^^^^^     const size_t a; }; 

and here live example.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -