🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #1823

Re: Usage of STL maps with pointers to member functions

#1823
From: Bernd Eggink
Date: Wed, 23 Aug 1995 12:42
60 lines
2099 bytes
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. ]
Technical Details
Message-ID: <41eniq$db3@rzsun02.rrz.uni-hamburg.de>
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back
References: <9508221303.AA23723@granados.sema.es>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back
Date: 23 Aug 95 12:42:10 GMT
Approved: fjh@cs.mu.oz.au
Return-Path: <daemon@ncar.UCAR.EDU>
From: admin@rzaix13.uni-hamburg.de (Bernd Eggink)
Newsgroups: comp.std.c++
Subject: Re: Usage of STL maps with pointers to member functions
X-Original-Date: 23 Aug 1995 08:06:50 GMT
Organization: University of Hamburg -- Germany
Message-ID: <41eniq$db3@rzsun02.rrz.uni-hamburg.de>
References: <9508221303.AA23723@granados.sema.es>
X-Newsreader: TIN [version 1.2 PL2]