473,785 Members | 2,482 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing pointer to member function to different member function that then calls for_each

Ok, this is what I am trying to do. I have a
member function that as a parameter accepts a
pointer to a different member function. In this 2nd
function, I want to iterate over an STL container
and call the passed in member function. I can do
this in my own loop, but I am looking to call
for_each. I am a little confused on the syntax to
use. Any help would be appreciated.

look at the following code and at the comment in
the printHold function

thanks in advance

#include <iostream>
#include <vector>
#include <string>

typedef std::vector<std ::string> Holder;

class Test
{
public:
typedef void (Test::*PRINTFU NC)(std::string );
void printHold(Holde r &hold, PRINTFUNC prn)
{
for(Holder::ite rator i=hold.begin(); i!=hold.end();+ +i)
(this->*prn)(*i);

// ************** LOOK HERE *************** **
// want to do something like this
// for_each(hold.b egin(),hold.end (),this->*prn);
// ************** LOOK HERE *************** **
}

// not always just a print function
void print(std::stri ng s)
{
cout << s;
}

void printFunc(Holde r &hold)
{
printHold(hold, &Test::print );
}

};

int main(int argc,char *argv[])
{
Holder h;
h.push_back("a" );
h.push_back("b" );

Test t;
t.printFunc(h);

return 0;
}

Jul 23 '05 #1
8 1439
Check out std::ptr_fun, std::mem_fun, and related functors.

</dib>

Jul 23 '05 #2
So what you are saying is that you do not know the answer?

This is NOT the standard question, but thanks for "trying"

Jul 23 '05 #3
Well, this turned out to be an interesting problem. A very interesting
problem indeed. The solution was not easily found. But I spent
several hours on it, and once I understood the nature of the problem
itself, I could figure out the best solution. I've come up with one
solution that I believe is pretty close to optimal - although I need to
do more testing, I am confident that the basis to the solution is
sound. Even failing that, I have also come up with alternate solutions
that are less than optimal, but certian to work.

Unfortunately for you, since you saw fit to be a pompous jerk in your
reply, you'll have to figure it out on your own.

Take care

</dib>

Jul 23 '05 #4
So what you are saying is that you do not know the answer?

Jul 23 '05 #5
And yet you continue to be a pompous jerk.

I do know the answer.

Take care.

</dib>

Jul 23 '05 #6
well keep on working on "optimizing " a "syntax error".

"And yet you continue to be a pompous jerk", funny that is what
everyone here is saying about you.

Do you get extra credit with your programming 101 teacher with every
"answer" you give on the web? Do you get more credit for getting it
correct? Thanks for answering, but if you are not going to read
questions and just put "read the faq" when you have no idea what the
question even is, then do not waste the bandwidth.

I have read your past posts and you seem to help people, but I guess
you have just become "one of those guys" that just get annoyed at
people that do not know what you know and you reply to every question
with "just read the manual idiot", when if you already know the answer
you can find it, but if you do not, then you can't. Just take a break
and maybe you will come back one day without the attitude.

Jul 23 '05 #7
> Thanks for answering, but if you are not going to read
questions and just put "read the faq" when you have no idea what the
question even is, then do not waste the bandwidth.


I did read the answer. Actually, I read it wrong. You're right about
that, and I did give you the wrong answer.

Originally when I gave you the wrong answer, you had a choice. You
could choose to be cool about it, and ask me to look at the problem
again. You could be sympathetic to the fact that I may have made an
honest mistake, even though I was genuinely trying to be helpful. You
might be constructive and enlist my help further.

Or you could be a jerk. Would you have said, "So what you are saying
is that you do not know the answer?" to your mother? Then you
shouldn't say it here. Just becasue you are anonymously hidden behind
some unknown email account doesn't make it OK to behave like a child.

You chose to be a jerk. If you choose to act like a child, I'll be
more than happy to treat you like one. I see no reason to help you.

Jul 23 '05 #8
this works for me

change:
for_each(hold.b egin(),hold.end *(),this->*prn);

to:
for_each(hold.b egin(),hold.end (),
std::bind1st(st d::mem_fun(prn) , this));

Of course I am still optimizing it.

John Dibling wrote:
Check out std::ptr_fun, std::mem_fun, and related functors.

</dib>


Jul 23 '05 #9

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

Similar topics

5
9377
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1, 2, func);
5
1970
by: BCC | last post by:
In looking through some code I have inherited, I notice a lot of places where the programmer used operator() as a function call: void operator() (int x, int y); Rather than an explicit function name: void MyFunction(int x, int y); Then when he instantiates a class he calls it: MyClass myclass; myclass (x, y);
37
5021
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined signature. When developing this lib, I figured that the pointer-to-member-function, although seemingly an attractive solution, does not work well for us.
6
8830
by: keepyourstupidspam | last post by:
Hi, I want to pass a function pointer that is a class member. This is the fn I want to pass the function pointer into: int Scheduler::Add(const unsigned long timeout, void* pFunction, void* pParam)
11
4432
by: cps | last post by:
Hi, I'm a C programmer taking my first steps into the world of C++. I'm currently developing a C++ 3D graphics application using GLUT (OpenGL Utility Toolkit written in C) for the GUI components. The application is built around a "World" object that contains a "GUI" object that is a C++ wrapper around GLUT. What I'd like to do is pass a pointer to a member function in the World class to function in the GUI
7
3307
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the object is a reference type? my code is not proving that. I have a web project i created from a web service that is my object: public class ExcelService : SoapHttpClientProtocol {
18
2879
by: tbringley | last post by:
I am a c++ newbie, so please excuse the ignorance of this question. I am interested in a way of having a class call a general member function of another class. Specifically, I am trying to write an ordinary differential equation class that would solve a general equation in the form: dx/dt = f(x,t). The ode class shouldn't know anything about f, except how to call it.
13
3137
by: arnuld | last post by:
i see the use of pointers, from K&R2 but what is the use of: 1. "pointer to pointer": char c; char** ppc; 2. pointer to function:
12
540
by: WaterWalk | last post by:
Hello. I am rather confused by the type of a pointer to class data member. Many c++ texts say that pointer to data member has a special syntax. For example the following class: class MyClass { public: int n; }; The pointer to MyClass.n shall be defined like this:
0
9645
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
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10324
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9949
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
8971
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...
0
6739
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4050
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
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.