How can I declare a pointer with filled information in C++? -
extern "c" { typedef struct pair_s { char *first; char *second; } pair; typedef struct pairofpairs_s { pair *first; pair *second; } pairofpairs; } pair pairs[] = { {"foo", "bar"}, //this fine {"bar", "baz"} }; pairofpairs pops[] = { {{"foo", "bar"}, {"bar", "baz"}}, //how can create equivalent of neatly {&pairs[0], &pairs[1]} //this not considered neat (imagine trying read list of 30 of these) }; how can achieve above style declaration semantics?
in c++11 write:
pairofpairs pops[] = { { new pair{"a", "a"}, new pair{"b", "b"} }, { new pair{"c", "c"}, new pair{"d", "d"} }, // grouping braces optional }; do note implications of using free store: objects allocated there not destructed @ end of execution of program (like static objects are) or anytime else (without corresponding delete). not concern in hosted implementations if pair c struct , not manage resources (and expected program use memory until exits).
edit: if can't use c++11 features, can create helper function. example:
static pair* new_pair(const char* first, const char* second) { pair* pair = new pair; pair->first = first; pair->second = second; return pair; }
Comments
Post a Comment