Constructor initialization value reachthrough in nested class
-
How do i reach the initialization value from outer into inner class ?
How do i initialize the big array using also the constructor of the small array?
Is there an easier way instead of using sizeof all the time to get the size of the class properties. My first try initializes the big array with zero instead of the initialization value. Initialization of the small array works.class class1 { private: uint8_t arr1[8]; public: uint8_t getbyte(uint8_t idx1) { return arr1[idx1]; } class1() { } //Default constructor class1(uint8_t fillvalue) { memset( arr1, fillvalue, sizeof(arr1) ); } }; class class2 { private: class1 arr2[25][40]; public: class1& getsubarr(uint8_t idx_x, uint8_t idx_y) { return arr2[idx_y][idx_x]; } class2(uint8_t fillvalue) { for (uint8_t y = 0; y < sizeof(arr2) / sizeof(arr2[0]) / sizeof(arr2[0][0]); y++){ for (uint8_t x = 0; x < sizeof(arr2[0]) / sizeof(arr2[0][0]); x++){ arr2[y][x] = class1(fillvalue);//temporary object } } } };
-
edit: I can give you a better answer in a moment
edit2: Or maybe I cannot.
Well at least nothing that is better than what you already have.Thus, just the answers to your questions:
How do i reach the initialization value from outer into inner class ?
You cannot do this when dealing with arrays. You either have to directly initialize each element (all 40*25) using an initializer list or they are constructed using the default constructor and you can assign a value later.
How do i initialize the big array using also the constructor of the small array?
Same as above.
Is there an easier way instead of using sizeof all the time to get the size of the class properties.
Yes! Use std::array instead of raw arrays. Then you can use std::fill with begin() and end().
-
Referring to your answer:
You mean something like following code?
#include <array> using namespace std; class someclass { private: array <uint8_t, somesize> arr; public: .... };or
#include <array> class class2 { private: std::array <uint8_t, somesize> arr; public: .... };This seems not to be standard c++ as i get the error messages:
c:\program files (x86)\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options.
xyz.cpp:23:3: error: 'array' in namespace 'std' does not name a type
Is it safe to use:
-std=c++0x
on GCC
or should i use another standard conform way?
How do i define two dimensional array classes?
Simply:
std::array <uint8_t, somesize, someothersize> arr;?
-
std::array is official standard since C++11 (from 2011). As your GCC told you, you usually have to activate compiler support for C++11 but by now all major compiler developers have long since implemented all major features of C++11. A very similiar class is std::tr1::array that was standardized 2003 in the technical report #1.
(If you really need to, std::array is quite trivial to implement. It is basically just
template <typename T, std::size_t N> class array { private: T data[N]; public: // Stuff for access and utility };)
You can declare two dimensional arrays by nesting one dimensional arrays:
array<array<class1>, 40>, 25> foo;You might want to use a typedef.