473,406 Members | 2,707 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,406 software developers and data experts.

pointer to a method

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

ptrMethodFoo : procedure

how do you do this in C++?
Jul 19 '05 #1
6 7908
xsist10 <xs*****@somewhere.com> wrote in message
news:10**************@skink.ru.ac.za...
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

Jul 19 '05 #2

"xsist10" <xs*****@somewhere.com> wrote in message
news:10**************@skink.ru.ac.za...
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

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

In article <Ek****************@newsread4.news.pas.earthlink.n et>,
mk******@mkwahler.net says...

"xsist10" <xs*****@somewhere.com> wrote in message
news:10**************@skink.ru.ac.za...
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

Jul 19 '05 #4
Woodster wrote:
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;
}

Jul 19 '05 #5
"Woodster" <mi****@127.0.0.1> wrote in message
news:MP************************@news.westnet.com.a u...
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

Jul 19 '05 #6
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");
}
}
Jul 19 '05 #7

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

Similar topics

2
by: lawrence | last post by:
I had some code that worked fine for several weeks, and then yesterday it stopped working. I'm not sure what I did. Nor can I make out why it isn't working. I'm running a query that should return 3...
5
by: lawrence | last post by:
I posted before, but have now narrowed my problem down to this method. At the start of the method, I test to make sure that I have a resource, a pointer to data returned from a database. This test...
6
by: Abhijit Deshpande | last post by:
Is there any elegant way to acheive following: class Base { public: Base() {} virtual ~Base() {} virtual void Method() { cout << "Base::Method called"; return; } };
10
by: Simon | last post by:
I'm a js newbie trying to use some very simple js to call an ActiveX object's methods. I need to use a pointer to call an embedded ActiveX object's method to receive a number. As I understand it,...
1
by: Nick Bishop | last post by:
I have a problem where I call a method in a C++ class with a pointer which is a static member in that class. When I use a debugger, I see the pointer having a certain value, but when I step into...
3
by: Rennie deGraaf | last post by:
Let's say that I want to read a method pointer in from a stream. (I'm not saying that this is a good design idea, or that I actually have a reason to do this.) If I wanted to read in a function...
7
by: Marcelo | last post by:
Hi everybody, I don't understand why I am having a problem in this code. The problem is that my pointer *phist in main method, it is declared. Then I send the pointer to my method, and this...
8
by: nsharma78 | last post by:
Hi, I have a code as follows: class A { public: void print(){cout << "Magic" << endl;} };
27
by: Erik de Castro Lopo | last post by:
Hi all, The GNU C compiler allows a void pointer to be incremented and the behaviour is equivalent to incrementing a char pointer. Is this legal C99 or is this a GNU C extention? Thanks in...
16
by: Alex Vinokur | last post by:
Does it have to be? : sizeof (size_t) >= sizeof (pointer) Alex Vinokur email: alex DOT vinokur AT gmail DOT com http://mathforum.org/library/view/10978.html http://sourceforge.net/users/alexvn
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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.