What is the life time of a C++ data structure object? -
What is the life time of a C++ data structure object? -
suppose have have car.h define class called auto , , have implementation car.cpp implement class car, illustration car.cpp can :
struct helper { ... }; helper helpers[] = { /* init code */ }; car::car() {} char *car::getname() { .....} what life time of helpers array ? need static helper helpers[]; ? if have done bad practices, please allow me know.
any variable declared/defined in global / namespace scope has finish life time until code ends.
if want helper helpers[]; accessible within car.cpp should declare static; otherwise allow global. in other words,
helper helpers[]; // accessible everywhere if `extern`ed file static helper helpers[]; // accessible in `car.cpp` edit: as, @andrewdski suggested in comment below; should create helpers[] static variable since using within file; though helper not visible outside. in c++, if 2 exclusively different unit has same named global variables compiler silently create mess referring them same memory location.
c++ data-structures object-lifetime
Comments
Post a Comment