473,396 Members | 2,014 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.

pointers to members inside a class?

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
6 2184
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

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
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
"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
> 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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: tekenenSPAM | last post by:
I know that one cannot cast a non-static member function to a void *. (http://users.utu.fi/sisasa/oasis/cppfaq/pointers-to-members.html) However, I have a case where I need to call a certain...
1
by: Christian Nolte | last post by:
I want to implement dynamic calling of interface methods using pointers to members and have some trouble to get it right. An example of what I am talking about looks like that: ...
4
by: Oystein Haare | last post by:
Is it best to declare class member objects as pointers or not pointers? class A { public: A(int i); //.... };
3
by: m | last post by:
Hi, I'm reading Microsoft Visual C++ .NET Step by Step, Version 2003 and I found a sentence it says : "you can't have pointers to managed types as members of a __gc class.". Why ? Thanks,...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
20
by: Joe Van Dyk | last post by:
Is there some rule of thumb about when to use pointers to an object and when to use a reference* to an object when a class needs to have objects as data members? Example: class A { B* b_ptr;...
6
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
1
by: FNA access | last post by:
Hello all, I am somewhat new to C++, so please forgive my ignorance. I have an assignment that I am working on and while I do not want someone to do my homework for me, some advice to point me in...
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
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.