Monday 15 March 2010

Initialize C++ array member in template class -


I need to implement a secure array class that controls the index, which reaches the underlying array: / P>

  template & lt; Typename T, int N & gt; Class Saferrere {public: T & operator [] (int index) {emphasis (0 <= Index End and Index & lt; N); Return m_data [index]; } Private: TM_data [N]; };   

and instead bool a [3]; , now I see the SafeArray & lt; Bool, 3 & gt; A; .

How do I support the initial array like bool b [3] = {false}; ? I mean what I should do to get the b.m_data [] = {false, false, false} safe < Bool, 3 & gt; has been created?

I think I should add a constructor to SafeArray , but what would be a group of that constructor? Template parameter T can be anything, not necessarily bool . I'm using pre-C ++ 11.

I mean what I should do bm_data [] = { False, false, false} after secure Bool, 3 & gt; B; has been created?

I'm not sure that I understand your question correctly, but if I do, then you just have to write a default constructor which is your starting array: < Pre> safe (): m_data () {}

Full code:

  template & lt; Typename t, int n & gt; Class safe (public: saferere): m_data () {} T & operator [] (int index) {emphasis (0 <= index & amp; index>   

and a

No comments:

Post a Comment