pointer to a method

X

xsist10

whats the best method. delphi lets you do something very simple

ptrMethodFoo : procedure

how do you do this in C++?
 
D

David White

xsist10 said:
whats the best method. delphi lets you do something very simple

ptrMethodFoo : procedure

how do you do this in C++?

You might need to ask the question more precisely. I'm only guessing what
you want.

class C
{
public:
void f();
void g();
};

int main()
{
C c;
void (C::*p)() = &C::f;
(c.*p)(); // calls C::f
p = &C::g;
(c.*p)(); // calls C::g
};

Here, 'p' is a pointer to a void non-static member function of C that takes
no parameters.

DW
 
M

Mike Wahler

xsist10 said:
whats the best method.

Best for what?
delphi lets you do something very simple

Delphi is not C++.
ptrMethodFoo : procedure

how do you do this in C++?

#include <iostream>

class C
{
public:
void memberfunc()
{
std::cout << "Hello\n";
}
};

int main()
{
void (C::*p)() = &C::memberfunc;
(C().*p)(); /* prints "Hello" */
return 0;
}



What are you specifically trying to do?

Which C++ book(s) are you reading?

-Mike
 
W

Woodster

I was trying to do something similar where I have several derived
classes each containing it's own specific member variables.

I am trying to construct a report consisting of values extracted from my
derived classes, where the report is entered as XML and I just extract
custom tags and replace them with a value. The way I wanted to do this
was to construct a list of name-value pairs, where the name is the tag I
am searching for and the value is a pointer to a method from one of the
derived classes. I do not know which classes the values will be coming
from until I reach the desired tag.

How would/should I declare a structure that will simply store a string
value (char *) and a function * as required so I couold call a specific
class method based on one of these string values?

Any help would be appreciated.

Thanks in advance

Woodster
 
R

Rolf Magnus

Woodster said:
I was trying to do something similar where I have several derived
classes each containing it's own specific member variables.

I am trying to construct a report consisting of values extracted from
my derived classes, where the report is entered as XML and I just
extract
custom tags and replace them with a value. The way I wanted to do
this was to construct a list of name-value pairs, where the name is
the tag I am searching for and the value is a pointer to a method from
one of the derived classes.

Why not use std::map with an std::string and a pointer to the object?
You can use virtual member functions then. No need for pointers to
members.

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

class base
{
public:
virtual void do_something() const = 0;
virtual ~base() {};
};

class derived1 : public base
{
public:
virtual void do_something() const;
};

class derived2 : public base
{
public:
virtual void do_something() const;
};

void derived1::do_something() const
{
std::cout << "derived 1\n";
}

void derived2::do_something() const
{
std::cout << "derived 2\n";
}

int main()
{
typedef std::map<std::string, base*> objmap;
objmap mymap;
mymap.insert(std::make_pair("foo", new derived1()));
mymap.insert(std::make_pair("bar", new derived2()));

std::string s;
std::cout << "Give a string: ";
std::getline(std::cin, s);

objmap::iterator it = mymap.find(s);
if (it != mymap.end())
it->second->do_something();
else
std::cout << "no operation defined for that string\n";

for (it = mymap.begin(); it != mymap.end(); ++it)
delete it->second;
}
 
D

David White

Woodster said:
I was trying to do something similar where I have several derived
classes each containing it's own specific member variables.

I am trying to construct a report consisting of values extracted from my
derived classes, where the report is entered as XML and I just extract
custom tags and replace them with a value. The way I wanted to do this
was to construct a list of name-value pairs, where the name is the tag I
am searching for and the value is a pointer to a method from one of the
derived classes. I do not know which classes the values will be coming
from until I reach the desired tag.

How would/should I declare a structure that will simply store a string
value (char *) and a function * as required so I couold call a specific
class method based on one of these string values?

A pointer-to-member is specific to a single class (the class named in the
pointer's declaration), so you can't have a Base::* pointing to a member of
Derived. However, if all value member functions are declared virtual in
Base, you could do everything with pointers-to-members of Base.

Otherwise, you could associate each name with a proxy object or function,
and the proxy can call the appropriate member function of the appropriate
class of object. For example, the hierarchy proposed in Rolf Magnus's reply
could be used as a proxy hierarchy if it's not suitable as the value
hierarchy itself. Each class's override would have a hard-wired call to a
specific member function of a member object of the appropriate derived
class. That's more trouble than I'd like to go to if I could avoid it,
though.

A cruder, un-OO, approach is to associate each name with an enum. A switch
on the enum would then call the appropriate member function. This would get
messy if an object pointer needed to be kept along with the enum, because
you'd have to cast the pointer to the correct derived-class pointer in the
switch.

DW
 
N

Nirus

xsist10 wrote:

I'm going to assume you mean a pointer to a function, since the "method"
keyword is not something used in C++ very often.
whats the best method. delphi lets you do something very simple

I can't really think of a decent reason to want to do this anyway, more than
that I'm not sure its supported in C++, one thing that has been added to
C++ (not sure how long ago) is templates, these can be used to define
different values as part of functions and classes.
ptrMethodFoo : procedure

how do you do this in C++?

Regardless of this fact I did a search and came up with this following piece
of code, I've got to be honest and say that I didn't write this, it can be
found at the following address
krtkg1.rug.ac.be/~colle/C/function_pointers.html:

typedef struct {
char *name;
int (*function)(char *, char *);
} command_type;

int do_command1(char *, char *);
int do_command2(char *, char *);

command_type command_list[]={
{"command1", do_command1 },
{"command2", do_command2 },
{NULL, NULL } /*we end the list with a NULL*/
};

main()
{
while (1)
{
command_type *command;
char buf[BUFSIZ];

if (NULL == gets(buf))
break; /*end of input*/

/*check if the command is in the list*/
for(command=command_list; command->name != NULL; command++)
if (!strcmp(command->name, buf))
/*yes it is -- execute associated code*/
command->function("argument1", "argument2");
}
}
 

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
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top