473,799 Members | 3,229 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(doubl e(*func)(double ), double a, double b);
double foo1(double t);

public:
double drive();

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

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

double Funct::foo1(dou ble 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 7540

"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(doubl e(*func)(double ), double a, double b);
double foo1(double t);

public:
double drive();

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

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

double Funct::foo1(dou ble 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::*)(doub le).
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(doubl e(*func)(double ), double a, double b);


// Try the following overload:

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

// which you define as

double Funct::Integrat e(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(doub le (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(dou ble t)
{
return t*t;
}

double Funct::Quadratu re(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::Quadratu re(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
3532
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 by pointer in C++, is there such way in Python? Thansk in advance. J.R.
5
3151
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 (count = 0; count <= 1; count++) { if (eval(f.RadioButtons.checked)) { btnChosen = true; }
17
7388
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 following code fragments in which I want to list through all files on a directory with a link to download each file by passing a filename and whether or not to force the download dialog box to appear.
1
7679
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 the bottom of this email)
14
20414
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 passed also. I understand that this way of passing the array is by value and if the prototype is declared as foo(int *), it is by reference in which case the value if modified in the function will get reflected in the main function as well. I dont...
28
4717
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
3847
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 bstruct, or cstruct, to the function? Sort of like this: myfunc(char * str, struct astruct *as)
6
3635
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 it gave a syntax error ("Invalid syntax") with caret pointing to the 'd' of the def keyword.
6
2714
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 pass by reference in C since you are just passing an address (or a pointer value address I guess?). So I was wondering is this correct?
24
55242
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 EventHandler(Onbutton_click); I want to pass more information related that event. & want to use that
0
9687
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
9541
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
10482
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...
1
10225
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,...
1
7564
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
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
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.