By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,474 Members | 3,139 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,474 IT Pros & Developers. It's quick & easy.

pointers to members inside a class?

P: n/a
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.
Jul 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Sun, 21 Sep 2003 18:37:43 GMT, Robert Dormer <rd*****@drexel.edu> wrote:
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)?

Jul 19 '05 #2

P: n/a

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?
Jul 19 '05 #3

P: n/a
Robert Dormer wrote:
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?


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

Jul 19 '05 #4

P: n/a
"Robert Dormer" <rd*****@drexel.edu> wrote in message
news:3r*****************@nwrdny03.gnilink.net...

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?


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[s])();
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
Jul 19 '05 #5

P: n/a
> 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
Jul 19 '05 #6

P: n/a
"Joe Simon" <jb******@lmco.com> wrote in message
news:bk*********@cui1.lmms.lmco.com...
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...


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<int>', etc.

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
Jul 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.