hi joaquin,
a)
here's a ready to use code snippet (see below).
i isolating the problem by writing the snippet to the reuse of objects or using local temporary objects;
i thought multi_index maps store copies of data objects, that where inserted.
am i wrong?
b)
why it isn't possible to write (with "?" is a class attribute or template parameter; see snippet):
template
const ELM *tCanMap
mClass00;
typedef mClass00::nth_index<0>::type mClass00ByNme;
typedef mClass00ByNme::iterator mClass00ByNmeItr;
typedef mClass00::nth_index<1>::type mClass00ByDat;
typedef mClass00ByDat::iterator mClass00ByDatItr;
//------------------------------------------------------------------------------
class cClass01 : public tCanMap {
private:
STRING _nme;
UINT32 _dat;
public:
cClass01() {}
cClass01(STRING nme, UINT32 dat) { this->_nme=nme; this->_dat=dat; this->_scNme = &(this->_mpMap.get<0>()); this->_scDat = &(this->_mpMap.get<1>()); }
STRING getnme() const { return this->_nme; }
UINT32 getdat() const { return this->_dat; }
#if defined (VERSION03) || defined (VERSION04)
const cClass00 *getmapelm (STRING stNme) const {
try { if ( this!=NULL ) { mClass00ByNmeItr itr = this->_mpMap.get<0>().find(stNme); if ( itr != this->_mpMap.get<0>().end() ) { return &(*itr); } } }
catch(...) {}
return NULL;
}
const cClass00 *getmapelm (UINT32 uiDat) const {
try { if ( this!=NULL ) { mClass00ByDatItr itr = this->_mpMap.get<1>().find(uiDat); if ( itr != this->_mpMap.get<1>().end() ) { return &(*itr); } } }
catch(...) {}
return NULL;
}
#endif
};
typedef boost::multi_index_container <
cClass01,
boost::multi_index::indexed_by <
boost::multi_index::hashed_unique < BOOST_MULTI_INDEX_CONST_MEM_FUN(cClass01,STRING,getnme) >,
boost::multi_index::ordered_unique < BOOST_MULTI_INDEX_CONST_MEM_FUN(cClass01,UINT32,getdat) >
mClass01;
typedef mClass01::nth_index<0>::type mClass01ByNme;
typedef mClass01ByNme::iterator mClass01ByNmeItr;
typedef mClass01::nth_index<1>::type mClass01ByDat;
typedef mClass01ByDat::iterator mClass01ByDatItr;
//------------------------------------------------------------------------------
class cClass02 : public tCanMap {
public:
cClass02() { this->_scNme = &(this->_mpMap.get<0>()); this->_scDat = &(this->_mpMap.get<1>()); }
#if defined (VERSION03) || defined (VERSION04)
const cClass01 *getmapelm (STRING stNme) const {
try { if ( this!=NULL ) { mClass01ByNmeItr itr = this->_mpMap.get<0>().find(stNme); if ( itr != this->_mpMap.get<0>().end() ) { return &(*itr); } } }
catch(...) {}
return NULL;
}
const cClass01 *getmapelm (UINT32 uiDat) const {
try { if ( this!=NULL ) { mClass01ByDatItr itr = this->_mpMap.get<1>().find(uiDat); if ( itr != this->_mpMap.get<1>().end() ) { return &(*itr); } } }
catch(...) {}
return NULL;
}
#endif
};
//------------------------------------------------------------------------------
int main(void) {
#if defined (VERSION01) || defined (VERSION03)
// works fine in any case
cClass00 objClass00_01 = cClass00( "elm00_01", 1 );
cClass00 objClass00_02 = cClass00( "elm00_02", 2 );
cClass00 objClass00_03 = cClass00( "elm00_03", 3 );
cClass00 objClass00_04 = cClass00( "elm00_04", 4 );
cClass01 objClass01_01 = cClass01( "elm01_01", 11 );
cClass01 objClass01_02 = cClass01( "elm01_02", 12 );
objClass01_01.setmapelm( objClass00_01 );
objClass01_01.setmapelm( objClass00_02 );
objClass01_02.setmapelm( objClass00_03 );
objClass01_02.setmapelm( objClass00_04 );
cClass02 *ptrClass02_01 = new cClass02();
ptrClass02_01->setmapelm( objClass01_01 );
ptrClass02_01->setmapelm( objClass01_02 );
printf("START\n");
printf("\n");
printf( "1. try: %02d\n", ptrClass02_01->getmapelm("elm01_01")->getdat() );
printf( "2. try: %02d\n", ptrClass02_01->getmapelm("elm01_02")->getdat() );
printf("\n");
printf( "3. try: %02d\n", ptrClass02_01->getmapelm("elm01_01")->getmapelm("elm00_01")->getdat() );
printf( "4. try: %02d\n", ptrClass02_01->getmapelm("elm01_02")->getmapelm("elm00_04")->getdat() );
printf("\n");
printf( "5. try: %s\n", ptrClass02_01->getmapelm("elm01_01")->getmapelm(2)->getnme().c_str() );
printf( "6. try: %s\n", ptrClass02_01->getmapelm("elm01_02")->getmapelm(3)->getnme().c_str() );
printf("\n");
printf("DONE\n");
#elif defined (VERSION02) || defined (VERSION04)
// run into segvault if object become overwritten OR
// object for filling multi_index is only a local variable of a filling function
cClass02 *ptrClass02 = new cClass02();
cClass01 objClass01;
cClass00 objClass00;
objClass01 = cClass01("elm01_01", 11);
objClass00 = cClass00("elm00_01", 1); objClass01.setmapelm(objClass00);
objClass00 = cClass00("elm00_02", 2); objClass01.setmapelm(objClass00);
objClass01 = cClass01("elm01_02", 12);
objClass00 = cClass00("elm00_03", 4); objClass01.setmapelm(objClass00);
objClass00 = cClass00("elm00_04", 3); objClass01.setmapelm(objClass00);
ptrClass02->setmapelm(objClass01);
printf("START\n");
printf( "dat = %d\n", ptrClass02->getmapelm("elm01_01")->getdat() ); // seg_vault
// printf( "dat = %d\n", ptrClass02->getmapelm("elm01_02")->getdat() ); // works fine
printf("DONE\n");
#endif
}
//--- END ----------------------------------------------------------------------
______________________________________________________
Bis 50 MB Dateianhänge? Kein Problem!
http://freemail.web.de/club/landingpage.htm/?mc=025556