473,748 Members | 7,827 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7926
xsist10 <xs*****@somewh ere.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*****@somewh ere.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::memberfu nc;
(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.earthl ink.net>,
mk******@mkwahl er.net says...

"xsist10" <xs*****@somewh ere.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::memberfu nc;
(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_so mething() const
{
std::cout << "derived 1\n";
}

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

int main()
{
typedef std::map<std::s tring, base*> objmap;
objmap mymap;
mymap.insert(st d::make_pair("f oo", new derived1()));
mymap.insert(st d::make_pair("b ar", new derived2()));

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

objmap::iterato r 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.au...
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.b e/~colle/C/function_pointe rs.html:

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

int do_command1(cha r *, char *);
int do_command2(cha r *, 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=com mand_list; command->name != NULL; command++)
if (!strcmp(comman d->name, buf))
/*yes it is -- execute associated code*/
command->function("argu ment1", "argument2" );
}
}
Jul 19 '05 #7

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

Similar topics

2
2268
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 items from the database. I then get a count of the return, which correctly tells me that I've got 3 items. I then go into a for loop which I expect to loop 3 times and print out the 3 items. Here is where things get strange: it loops 3 times and...
5
2035
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 is coming back true, so the next line runs, which attempts to get the next row from the dataset. This brings back nothing. On the queries I'm running right now, the first row will be fetched, but then no further rows. If I expect 20 rows back, I...
6
5638
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
4389
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, js is typeless so how can I get a variable to be a pointer type? Passing var i as the parameter gets an undefined value in return. Thanks IA, Simon
1
1917
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 the method (F11), the pointer has a different value and is not pointing at anything valid. The small program (complete source below) demonstrates what I am trying to achieve. The small program works perfectly, but when I incorporate this logic...
3
1988
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 pointer, I could do something like this: #include <iostream> int*(*readFunction())(int, int*) { unsigned long x;
7
2046
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 method creates a new object (a Matrix) for it. I suppose that after the new operator, my pointer is pointing to an object, so when the method has finished, the very first pointer is still poitint to the created method; however this is not working,...
8
3349
by: nsharma78 | last post by:
Hi, I have a code as follows: class A { public: void print(){cout << "Magic" << endl;} };
27
8968
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 advance. Erik
16
15655
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
8987
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9366
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9316
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9241
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8239
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3303
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2211
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.