[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference turris::c_plus_plus

Title:C++
Notice:Read 1.* and use keywords (e.g. SHOW KEY/FULL KIT_CXX_VAX_VMS)
Moderator:DECCXX::AMARTIN
Created:Fri Nov 06 1987
Last Modified:Thu Jun 05 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:3604
Total number of notes:18242

3440.0. "ACCVIO getting object pointer from STL map" by CIM2NI::THORPE () Mon Feb 10 1997 14:14

I'm getting an access violation (look for the "ACCVIO here" near the
end of this code sample.

Basically I have a component manager class (BlahClassMgr) which
manages an STL map.  I would like to use the "lookup" member function
to return an object reference based on an input key.  If the key can
be found in the map then I would like to return, thru the arg list, a
pointer to the object referenced by the key.

I'm also curious why the "lookup" function cannot be "const".

Thanks in advance,
Bill
=======================================
// b.h
#include <map>

class BlahClass
{
friend ostream &operator<< ( ostream &stream ,const BlahClass &object );
private:
  int attr1;
  int attr2;
public:
  BlahClass (int a=0, int b=0) {attr1=a, attr2=b;}
  get_attr1 ( void ) { return this->attr1;}
  set_attr1 ( int b ) { this->attr1 = b;}
} *aBlahClassPtr;

// Insertor
  ostream &
  operator<< ( ostream &stream ,const BlahClass &object )
  {
    stream << "attr1: " << object.attr1 << "  attr2: " << object.attr2;
    return stream;
  }

typedef map<int, BlahClass, less<int> > list_t;

class BlahClassMgr
{
private:
  list_t blahs;
public:
  BlahClassMgr ( void )
    {
    BlahClass b1(1,11);
    BlahClass b2(2,22);
    BlahClass b3(3,33);
    blahs [ 1 ] = b1;
    blahs [ 2 ] = b2;
    blahs [ 3 ] = b3;
    for (list_t::iterator i=blahs.begin(); i != blahs.end(); i++)
      cout << (*i).second << endl;
    }

  bool lookup ( BlahClass *aBlah, const int &aKey )
    {
    list_t::iterator i;
    if ( (i = (this->blahs).find ( aKey )) == (this->blahs).end () )
      return FALSE;
    aBlah = &(*i).second;
    return TRUE;
    }
};

============================================
// b.cxx
#include "b.h"

main( void )
{
  BlahClassMgr aBlahClassMgr;

  bool joy = aBlahClassMgr.lookup ( aBlahClassPtr, 2 );

  if (joy)
    {
    cout << *aBlahClassPtr << endl;   // ACCVIO here
    aBlahClassPtr->set_attr1(99);     // ACCVIO here too
    }
  else
    cout << "Bummer man!" << endl;
}
T.RTitleUserPersonal
Name
DateLines
3440.1SPECXN::DERAMODan D'EramoMon Feb 10 1997 14:5331
>  bool lookup ( BlahClass *aBlah, const int &aKey )
>    {
>    list_t::iterator i;
>    if ( (i = (this->blahs).find ( aKey )) == (this->blahs).end () )
>      return FALSE;
>    aBlah = &(*i).second;
>    return TRUE;
>    }
>};

>  bool joy = aBlahClassMgr.lookup ( aBlahClassPtr, 2 );

        The above does not result in an assignment to the variable
        aBlahClassPtr.  You would need to declare the first argument
        to lookup as a reference to a "BlahClass *"...
        
        	bool lookup ( BlahClass * &aBlah, const int &aKey )
        
        ...in order for the call...
        
        	aBlahClassMgr.lookup ( aBlahClassPtr, 2 )
        
        ...to result in a modification to aBlahClassPtr.
        
>I'm also curious why the "lookup" function cannot be "const".
        
        You probably can't assign the const_iterator returned by
        "find(const key_type &) const" to your iterator variable
        "i".
        
        Dan
3440.2thanksCIM2NI::THORPEMon Feb 10 1997 15:314
That works.

Thanks,
Bill