pointers to members inside a class?

R

Robert Dormer

Is it possible to declare pointers to a member function inside the class
containing the member function? I.E.

class some_class{

some_class::* ptr;


};

I've tried to do something similar to this but gcc gives me an error. The
class in question is a virtual class. Can one declare anything like this?

class some_class{

map <string, some_class::*> some_important_map;

};


And if so, how does one do it?

Thanks in advance.
 
A

Alf P. Steinbach

Is it possible to declare pointers to a member function inside the class
containing the member function? I.E.

class some_class{

some_class::* ptr;


};

What is the _real_ problem (not the assumed technical solution)?


I've tried to do something similar to this but gcc gives me an error. The
class in question is a virtual class.

C++ has no such thing as "virtual class".


Can one declare anything like this?

class some_class{

map <string, some_class::*> some_important_map;

};

That's invalid syntax.

Again, what is the _real_ problem (not the assumed technical solution)?
 
R

Robert Dormer

What is the _real_ problem (not the assumed technical solution)?


Okay, by this I assume you mean what am I trying to solve? I have a set of
strings that are going to be coming in over a socket, and rather than using
a giant case statement, I'd rather have a map - the strings mapped to
member pointer functions. That what you're looking for?

Why is the syntax incorrect?
 
N

Noah Roberts

Robert said:
Okay, by this I assume you mean what am I trying to solve? I have a set of
strings that are going to be coming in over a socket, and rather than using
a giant case statement, I'd rather have a map - the strings mapped to
member pointer functions. That what you're looking for?

Why is the syntax incorrect?

class some_class
{
std::map< std::string, retval (some_class::*)(parameter_list) >
command_map;
};
 
M

Mike Wahler

Robert Dormer said:
Okay, by this I assume you mean what am I trying to solve? I have a set of
strings that are going to be coming in over a socket, and rather than using
a giant case statement, I'd rather have a map - the strings mapped to
member pointer functions. That what you're looking for?

Why is the syntax incorrect?

A function has a (required) return type and a
(required but optionally empty) argument list.
A pointer to a function must specify those.

#include <iostream>
#include <map>
#include <string>

class X
{
void f1() { std::cout << "f1\n"; }
void f2() { std::cout << "f2\n"; }
void f3() { std::cout << "f3\n"; }

std::map<std::string, void (X::*)()> table;

public:
X()
{
table["Tom"] = &X::f1;
table["Dick"] = &X::f2;
table["Harry"] = &X::f3;
}

void dispatch(const std::string& s)
{
if(table.find(s) != table.end())
(this->*table)();
else
std::cout << s << " not found\n";
}


};

int main()
{
X x;
x.dispatch("Tom");
x.dispatch("Dick");
x.dispatch("Mary");

return 0;
}

Output:

f1
f2
Mary not found

HTH,
-Mike
 
J

Joe Simon

class X
{
void f1() { std::cout << "f1\n"; }
void f2() { std::cout << "f2\n"; }
void f3() { std::cout << "f3\n"; }

std::map<std::string, void (X::*)()> table;

ohhh headache, i'm having trouble parsing this...

type is std::map,

The parameter pairs are std::string and void(X::*)()

The name of the map is table.

So far so good.
Now, the parameter : first is the key, std::string,
second is void(X::*) (). Thid is a function declaration.

return value : void.
function Name : X::* ... A pointer to something in the X class ? I
do not unnerstand this. Can someone elaborate please ?
Parameters : () : none

<snip>

thanks
Joe
 
M

Mike Wahler

Joe Simon said:
ohhh headache, i'm having trouble parsing this...

It takes a while to get used to it. :)
type is std::map,

No, type is std::map<std::string, void (X::*)()>

(the template arguments are part of the type).

E.g. there's no 'vector' type, but there can
be 'vector said:
The parameter pairs are std::string and void(X::*)()

What 'pairs?'. The two types supplied as template arguments
are those two types you mention.
The name of the map is table.
Yes.


So far so good.
Now, the parameter : first is the key, std::string,

The first template argument to 'map' is the type, 'std::string'.
This is the type used for the map's 'key'.
second is void(X::*) (). Thid is a function declaration.

No, it specifies the type of a pointer to a member function.
It's not a declaration.
return value : void.

That's the return type of the pointed to function.
function Name : X::*

No. The parameter is a type. There's no 'function name'.
'X' is the name of the class whose member the pointer will
point to. The '::' is the 'scope resolution' operator.
'X::' is part of the pointer type.

... A pointer to something in the X class ?

Yes, to a member (of class 'X') function which has a
return type of 'void', and takes no arguments.
I
do not unnerstand this. Can someone elaborate please ?

Hope that helped. But your questions indicate you need
some good textbooks. Are you reading any? Which ones?
There are good recommendations at the www.accu.org site.

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top