Topic: Usage of STL maps with pointers to member functions
Author: Gabriel Sanchez Gutierrez <gsg@sema.es>
Date: 1995/08/22 Raw View
Hello,
As far as I know, maps are transparent wrt the keys and values stored.
I am trying to maintain a map with pointers to member functions. If I access
the members through normal pointers, all is Ok. But if I use the dictionary,
it seems it is not properly initialized.
Any hint?
#include <iostream.h>
#include <map.h>
typedef char* string;
class Test {
typedef int (Test::* pGetM) ();
public:
int a1;
int geta1 () {return a1;};
public:
Test (int a): a1(a) {};
};
main ()
{
Test t (5);
Test::pGetM f = Test::geta1;
map<string, Test::pGetM, less<string> > mapMethods;
mapMethods["a1"] = f;
cout << "Attribut = " << t.a1 << endl
<< "By map = " << (t.*f)() << endl;
cout << "Attribut = " << t.a1 << endl
<< "By map = " << (t.*mapMethods["a1"])() << endl;
}
--
--------------------------------------------------------------------------
Gabriel Sanchez Gutierrez | Phone: +34.1.327.28.28 x204
DIS - Sema Group sae | Fax: +34.1.754.32.52
Albarracin, 25 | Email: gsg@sema.es
28037 Madrid - SPAIN | WWW: http://www.sema.es/~gsg
---
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Author: admin@rzaix13.uni-hamburg.de (Bernd Eggink)
Date: 1995/08/23 Raw View
Gabriel Sanchez Gutierrez (gsg@sema.es) wrote:
> Hello,
> As far as I know, maps are transparent wrt the keys and values stored.
> I am trying to maintain a map with pointers to member functions. If I access
> the members through normal pointers, all is Ok. But if I use the dictionary,
> it seems it is not properly initialized.
> Any hint?
> #include <iostream.h>
> #include <map.h>
> typedef char* string;
> class Test {
> typedef int (Test::* pGetM) ();
> public:
> int a1;
> int geta1 () {return a1;};
> public:
> Test (int a): a1(a) {};
> };
> main ()
> {
> Test t (5);
> Test::pGetM f = Test::geta1;
>
> map<string, Test::pGetM, less<string> > mapMethods;
> mapMethods["a1"] = f;
> cout << "Attribut = " << t.a1 << endl
> << "By map = " << (t.*f)() << endl;
> cout << "Attribut = " << t.a1 << endl
> << "By map = " << (t.*mapMethods["a1"])() << endl;
> }
You are comparing pointers instead of strings. Add
struct less<string>
{
bool operator()(string a, string b) { return strcmp(a, b) < 0; }
};
and it will work.
--
+----------------------------------+
| Bernd Eggink |
| Rechenzentrum Uni Hamburg |
| admin@rzaix13.rrz.uni-hamburg.de |
+----------------------------------+
---
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]