initialization - c++ member array not initialized -
i have following constellation:
class base {...}; class derived : public base { public: unsigned int* a; derived(size_t num = 1) : a(0), _b(0) { = new unsigned int[num]; _b = new unsigned char[num]; } private: unsigned char* _b; }
all fine doing this:
derived* instance = new derived();
but doing this:
base* instance = new derived();
_b stays 0x0 , errors occur later when try use _b.
what happening here?
update:
the problem caused windows / linux cross platform issue - values filled incorrect pointers.
first:
why need initialize a
, b
twice? use explicit initialization only, or standard member initialization not both...
derived(size_t num = 1) : a(new unsigned int[num]), _b(new unsigned char[num]) { }
this should trick.
second: if create instance of derived
base
, members , functions of derived useless. except members , functions of base, common in both classes.
Comments
Post a Comment