🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

2 total messages Started by Gabriel Sanchez Gutierrez Tue, 22 Aug 1995 14:01
Usage of STL maps with pointers to member functions
#1806
Author: Gabriel Sanchez Gutierrez
Date: Tue, 22 Aug 1995 14:01
51 lines
2009 bytes
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. ]
Re: Usage of STL maps with pointers to member functions
#1823
Author: 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. ]
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads