473,396 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Q: Map of pointers to member functions

Jim
I'd like to create a map of strings to member function pointers, so that I
can call a specific function based on a string. I'm compiling this using MS
Visual C++ 7.0 (VS.NET 2002)

class Foo;

/// Prototype for functions that extract a parameter from a module
typedef const void (Foo::*PARAM_GETTER)(std::string&);

/// A map of strings to parameter getters
typedef std::map<std::string, PARAM_GETTER> ParamGetterMap;

And then,

class Foo
{
protected:
ParamGetterMap m_getterMap;
};

class Bar : public AnotherBaseClass, public Foo
{
private:
// getter/setter helpers
void GetBar(std::string& strValue) { strValue = "something"; }
};

And finally,

Bar::Bar()
{
// Fill the parameter maps
m_getterMap.insert(make_pair("bar", GetBar)); // COMPLILE ERROR HERE
}

I get the following compile error:

C:\Microsoft Visual Studio .NET\Vc7\include\utility(41) : error C2440:
'initializing' : cannot
convert from 'void (__thiscall Bar::* const )(std::string &)' to
'PARAM_GETTER '
Types pointed to are unrelated; conversion requires reinterpret_cast,
C-style cast or
function-style cast
Bar.cpp(41) : see
reference to function template instantiation 'std::pair<_Ty1,_Ty2>::pair
(const std::pair<char,void
(__thiscall Bar::* )(std::string &)> &)' being compiled
with
[
_Ty1=const std::string,
_Ty2=PARAM_GETTER
]
C:\Microsoft Visual Studio .NET\Vc7\include\utility(41) : error C2439:
'std::pair<_Ty1,_Ty2>::second' : member could not be initialized
with
[
_Ty1=const std::string,
_Ty2=PARAM_GETTER
]
C:\Microsoft Visual Studio .NET\Vc7\include\utility(56) : see
declaration of
'std::pair<_Ty1,_Ty2>::second'
with
[
_Ty1=const std::string,
_Ty2=PARAM_GETTER
]

Can anyone point out what I'm doing wrong here?

Thx x 10^6

--
Jim Johnson
Jul 23 '05 #1
6 3303
Jim wrote:
m_getterMap.insert(make_pair("bar", GetBar)); // COMPLILE ERROR HERE
GetBar is only defined in Bar but PARAM_GETTER is a function pointer to
a member function of Foo:
typedef const void (Foo::*PARAM_GETTER)(std::string&);


I hope that helps you.
Jul 23 '05 #2
Jim
> Jim wrote:
m_getterMap.insert(make_pair("bar", GetBar)); // COMPLILE ERROR HERE


GetBar is only defined in Bar but PARAM_GETTER is a function pointer to
a member function of Foo:
> typedef const void (Foo::*PARAM_GETTER)(std::string&);


I hope that helps you.


A little; but it doesn't point to a solution.

I've used a similar technique before and it worked; I had to cast the
function pointer to a specific type to clear up a different error, however
here I can't get even that close (the cast gives me severe warning).

--
Jim Johnson
Jul 23 '05 #3
Jim wrote:
[...]
Bar::Bar()
{
// Fill the parameter maps
m_getterMap.insert(make_pair("bar", GetBar)); // COMPLILE ERROR HERE
The address of a non-static member function cannot be obtained by using
only its name. The format is &Bar::GetBar. It is required.
}

Jul 23 '05 #4
Jim
> Jim wrote:
[...]
Bar::Bar()
{
// Fill the parameter maps
m_getterMap.insert(make_pair("bar", GetBar)); // COMPLILE ERROR HERE


The address of a non-static member function cannot be obtained by using
only its name. The format is &Bar::GetBar. It is required.
}


Thanks; but using the syntax you specified makes no difference (same compile
error).

--
Jim Johnson
Jul 23 '05 #5
Jim wrote:
Jim wrote:
m_getterMap.insert(make_pair("bar", GetBar)); // COMPLILE ERROR HERE


GetBar is only defined in Bar but PARAM_GETTER is a function pointer to
a member function of Foo:
> typedef const void (Foo::*PARAM_GETTER)(std::string&);


I hope that helps you.

A little; but it doesn't point to a solution.

I've used a similar technique before and it worked; I had to cast the
function pointer to a specific type to clear up a different error, however
here I can't get even that close (the cast gives me severe warning).


No, what he's saying is your map translates from std::string to Foo::*
member.
You're trying to pass it a Bar::* member. The typedef in the error
message is hiding it. Look at the def of PARAM_GETTER, and the def of
GetBar().
Jul 23 '05 #6
Jim wrote:
Jim wrote:
[...]
Bar::Bar()
{
// Fill the parameter maps
m_getterMap.insert(make_pair("bar", GetBar)); // COMPLILE ERROR HERE


The address of a non-static member function cannot be obtained by using
only its name. The format is &Bar::GetBar. It is required.

}


Thanks; but using the syntax you specified makes no difference (same compile
error).


Sorry, solved a wrong problem :-)

A pointer to member of a derived class is not convertible to a pointer
to member of the base class. It does work that way for pointers to
objects, but works _in_reverse_ for pointers to members.

To convert a pointer to member you need a static_cast.
--------------------------------------------------------
Bar::Bar()
{
// Fill the parameter maps
m_getterMap.insert(std::make_pair("bar",
static_cast<PARAM_GETTER>(&Bar::GetBar))); // NO ERROR HERE
}
--------------------------------------------------------

V
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: xuatla | last post by:
Hi, I have the following code for function pointer. compiling is ok. Can you help me to check whether it's a good way to implement as: class CA { ..... private: void f1( double ) ;
4
by: Ian Malone | last post by:
I have a class which looks like this: class reco { public: int height, width; double beta; double mu; simple_d_field estimate; simple_d_field auxiliary;
7
by: jolie.demiranda | last post by:
Dear all, I'm fairly new to C++ so forgive me is this topic may have been convered before... I'm having a problem with pointers to member functions. Essentially what I want to achieve is the...
3
by: Bilgehan.Balban | last post by:
Hi, How do I declare an array of function pointers and assign each element of the array with public member functions of a class? Is it possible that the array is not a member of the class? ...
11
by: cps | last post by:
Hi, I'm a C programmer taking my first steps into the world of C++. I'm currently developing a C++ 3D graphics application using GLUT (OpenGL Utility Toolkit written in C) for the GUI...
12
by: addinall | last post by:
Hi guys and gals, Haven't been in here for a good decade or so! I'm having a braid-dead moment that is lasting for a couple of days. Declaring pointers to functions witrhin structures and...
5
by: Martijn van Buul | last post by:
Hi. I'm having a peculiar problem at work. I've been googling for it, but haven't found an authorative answer. In a nutshell (Long story follows), what I'd like to know is: If I have a C++...
22
by: sandy | last post by:
I am trying to make a simulated directory structure application for a course. I am using a Vector to store pointers to my Directory objects (as subdirectories of the current object). In my...
12
by: bgold | last post by:
Hey. I have a base class (SPRITE), and using this base class I have derived a large number of derived classes (PERSON, BULLET, MISSILE, etc.). Now, at a certain point in my program, I have a pair...
3
by: Ramesh | last post by:
Hi, I am trying to create an array of pointers to member functions inside my class. When I created a global array of type pfn & initialized with member functions and copy it back to the member...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.