472,372 Members | 1,780 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,372 software developers and data experts.

How to pass a function name as an argument?

I have a simple test program. It doesn't work. I want to know what is the
reason and how to
fix it. Maybe I should use template function to do it. But I don't know how.

Here is the simple program. Strange enough that the problem never happens in
C languge,
i.e., in the program, if I call "foo2", it works fine. Why "foo1" doesn't
work?

Thanks for your answers.
////////////////////////////////////////////////////////////////////////////
/
file://main.cpp
////////////////////////////////////////////////////////////////////////////
/
#include <stdio.h>
#include <stdlib.h>
#include "Funct.h"

void main()
{
double s;
Funct funct;
s = funct.drive();
printf("Result= %lf", s);
}

////////////////////////////////////////////////////////////////////////////
/
file://Funct.h
////////////////////////////////////////////////////////////////////////////
/
double foo2(double t);

class Funct
{
double Integrate(double(*func)(double), double a, double b);
double foo1(double t);

public:
double drive();

};
////////////////////////////////////////////////////////////////////////////
/
file://Funct.cpp
////////////////////////////////////////////////////////////////////////////
/
#include "Funct.h"

double Funct::Integrate(double (*func)(double), double a, double b)
{
double c = a+b;
return func(c);
}

double Funct::foo1(double t)
{
return t*t;
}

double Funct::drive()
{
double t = Integrate(foo1, 2, 3);
// double t = Integrate(foo2, 2, 3);
return t;
}

double foo2(double t)
{
return t*t;
}
**************************************
Jul 19 '05 #1
5 7446

"Yangang Bao" <yb**@ualberta.ca> wrote in message
news:bm**********@tabloid.srv.ualberta.ca...
I have a simple test program. It doesn't work. I want to know what is the
reason and how to
fix it. Maybe I should use template function to do it. But I don't know how.

Here is the simple program. Strange enough that the problem never happens in
C languge,
i.e., in the program, if I call "foo2", it works fine. Why "foo1" doesn't
work?

Thanks for your answers.
////////////////////////////////////////////////////////////////////////////
/
file://main.cpp
////////////////////////////////////////////////////////////////////////////
/
#include <stdio.h>
#include <stdlib.h>
#include "Funct.h"

void main()
{
double s;
Funct funct;
s = funct.drive();
printf("Result= %lf", s);
}

////////////////////////////////////////////////////////////////////////////
/
file://Funct.h
////////////////////////////////////////////////////////////////////////////
/
double foo2(double t);

class Funct
{
double Integrate(double(*func)(double), double a, double b);
double foo1(double t);

public:
double drive();

};
////////////////////////////////////////////////////////////////////////////
/
file://Funct.cpp
////////////////////////////////////////////////////////////////////////////
/
#include "Funct.h"

double Funct::Integrate(double (*func)(double), double a, double b)
{
double c = a+b;
return func(c);
}

double Funct::foo1(double t)
{
return t*t;
}

double Funct::drive()
{
double t = Integrate(foo1, 2, 3);
// double t = Integrate(foo2, 2, 3);
return t;
}

double foo2(double t)
{
return t*t;
}


See the first parameter of Integrate.
It is a pointer to a function that takes a double as parameter and returns a
double.
What is the type of foo1?
It is double (Funct::*)(double).
Pointers to non-static member functions are not type compatible with regular
pointers to functions, hence the error.

HTH,
J.Schafer
Jul 19 '05 #2
"Yangang Bao" <yb**@ualberta.ca> wrote in message
news:bm**********@tabloid.srv.ualberta.ca
I have a simple test program. It doesn't work. I want to know what is
the reason and how to
fix it. Maybe I should use template function to do it. But I don't
know how.

Here is the simple program. Strange enough that the problem never
happens in C languge,
The C language doesn't have member functions, so the issue never arises.
i.e., in the program, if I call "foo2", it works fine. Why "foo1"
doesn't work?
Because it is a member function, not a "free" function. The syntax for
pointers to member functions is very different to the syntax for pointers to
ordinary functions.

[snip]

class Funct
{
double Integrate(double(*func)(double), double a, double b);


// Try the following overload:

double Integrate(double(Funct::*func)(double), double a, double b);

// which you define as

double Funct::Integrate(double (Funct::*func)(double), double a, double b)
{
double c = a+b;
return (this->*func)(c);
}
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 19 '05 #3
It seems it works. You are really helpful. Thanks a lot.
Jul 19 '05 #4
I tried to adopt this method in my program. And unfortunately a tragedy
happens. The problem is that I have to pass the function name as argument
twice.
And the VC6.0 compiler still shows the wrong message: INTERNAL COMPILER
ERROR. I just wonder how other guys implemented the quadrature
functions in C++.

Here is the simple test program:
//////////////////////////////////////////////////////////////////////////
// main.cpp
//////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>

#include "Funct.h"

void main()
{
Funct funct;
funct.drive();
}

//////////////////////////////////////////////////////////////////////////
// Funct.h
//////////////////////////////////////////////////////////////////////////
#ifndef FUNCT_INCLUDED_
#define FUNCT_INCLUDED_

class Funct
{
public:
double Quadrature(double (Funct::*func)(double), double a, double b);
double QuadStep(double (Funct::*func)(double), double c, double d);

double foo1(double t);

double drive();
};

#endif

//////////////////////////////////////////////////////////////////////////
// Funct.cpp
//////////////////////////////////////////////////////////////////////////
#include "Funct.h"

double Funct::drive()
{
double a = 1;
double b = 2;
double s;

s = Quadrature(foo1, a, b);
return s;

}

double Funct::foo1(double t)
{
return t*t;
}

double Funct::Quadrature(double (Funct::*func)(double), double a, double b)
{
double c= (a+b)/2.0;
double s1, s2;

s1 = QuadStep((this->*func), a, c);
s2 = QuadStep((this->*func), c, d);
return s;
}

double Funct::QuadStep(double (Funct::*func)(double), double c, double d)
{
return (this->*func)(c*d);
}
"Yangang Bao" <yb**@ualberta.ca> wrote in message
news:bm**********@tabloid.srv.ualberta.ca...
It seems it works. You are really helpful. Thanks a lot.

Jul 19 '05 #5
On Tue, 14 Oct 2003 12:10:35 -0600, "Yangang Bao" <yb**@ualberta.ca>
wrote:
I tried to adopt this method in my program. And unfortunately a tragedy
happens. The problem is that I have to pass the function name as argument
twice.
And the VC6.0 compiler still shows the wrong message: INTERNAL COMPILER
ERROR. I just wonder how other guys implemented the quadrature
functions in C++. double Funct::Quadrature(double (Funct::*func)(double), double a, double b)
{
double c= (a+b)/2.0;
double s1, s2;

s1 = QuadStep((this->*func), a, c);
s2 = QuadStep((this->*func), c, d);
The above two should be:

s1 = QuadStep(func, a, c);
s2 = QuadStep(func, c, d);

You only dereference the pointer to member when calling it.
return s;
}

double Funct::QuadStep(double (Funct::*func)(double), double c, double d)
{
return (this->*func)(c*d);


That's fine, since you're calling it.

Tom
Jul 19 '05 #6

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

Similar topics

46
by: J.R. | last post by:
Hi folks, The python can only support passing value in function call (right?), I'm wondering how to effectively pass a large parameter, such as a large list or dictionary? It could achieved...
5
by: Seeker | last post by:
Newbie question here... I have a form with some radio buttons. To verify that at least one of the buttons was chosen I use the following code ("f" is my form object) : var btnChosen; for...
17
by: Fresh Air Rider | last post by:
Hello Could anyone please explain how I can pass more than one arguement/parameter value to a function using <asp:linkbutton> or is this a major shortfall of the language ? Consider the...
1
by: Mark Dicken | last post by:
Hi All I have found the following Microsoft Technet 'Q' Article :- Q210368 -ACC2000: How to Pass an Array as an Argument to a Procedure (I've also copied and pasted the whole contents into...
14
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is...
28
by: Bill | last post by:
Hello All, I am trying to pass a struct to a function. How would that best be accomplished? Thanks, Bill
12
by: Fred | last post by:
Is it possible to create a function which will take any number of structures as an argument? For example, what if I wanted depending on the circumstances to pass a structure named astruct, or...
6
by: vasudevram | last post by:
Hi group, Question: Do eval() and exec not accept a function definition? (like 'def foo: pass) ? I wrote a function to generate other functions using something like eval("def foo: ....") but...
6
by: lisp9000 | last post by:
I've read that C allows two ways to pass information between functions: o Pass by Value o Pass by Reference I was talking to some C programmers and they told me there is no such thing as...
24
by: =?Utf-8?B?U3dhcHB5?= | last post by:
Can anyone suggest me to pass more parameters other than two parameter for events like the following? Event: Onbutton_click(object sender, EventArgs e)" Event handler: button.Click += new...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

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.