473,396 Members | 1,921 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,396 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 7512

"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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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
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...
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,...
0
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...

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.