c++ - Initialise large static class array -


i have template create instance of various objects. template has static array of class declaration supposed create array of class type being passed during creation.

in below example myclass static array of class object size 200 - can bigger also. note template can instantiated different objects - type of array changed accordingly.

how can initialize static array during declaration - understand need initialize static array when defined itself, if size if more bigger -

template <class object> a<object> myclass[200] = { .... new object 200 times...}; 

or need new / delete overloaded operator defined in template? in such case how array of objects construction & destruction occur? if object references array static before template instantiation?

how can initialise static array during declaration [?]

if want initialize objects default (no parameter) contructor, it's easy; like

template <class object> object a<object>::myclass[200] { }; 

the following full (simplified) example

#include <iostream>  template <typename t, std::size_t dim> struct foo : public t   { static t const myarray[dim]; };  template <typename t, std::size_t dim> t const foo<t, dim>::myarray[dim] { };  struct bar  { bar () { std::cout << "bar! " << std::endl; } };  int main ()  { (void)foo<bar, 10>::myarray; } // print 10 times "bar!" 

if want initialize different constructor... it's little more complicated.

the following c++14 example (use std::index_sequence , std::make_index_sequence; isn't difficult substitute in c++11, if need it) use partial specialization, template default values, variadic parameters unpacking , comma operator

#include <utility> #include <iostream>  template <typename t, std::size_t dim,           typename u = decltype(std::make_index_sequence<dim>{})> struct foo;  template <typename t, std::size_t dim, std::size_t ... is> struct foo<t, dim, std::index_sequence<is...>>  { static t const myarray[dim]; };  template <typename t, std::size_t dim, std::size_t ... is> t const foo<t, dim, std::index_sequence<is...>>::myarray[dim]     { ((void)is, t(1))... };  struct bar  { bar (int) { std::cout << "bar! " << std::endl; } };  int main ()  { (void)foo<bar, 10>::myarray; } // print 10 times "bar!" 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -