473,378 Members | 1,523 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,378 software developers and data experts.

Using a pointer to member function

Hello. I'm trying to make a sort of generic integral class, holding
the boundary values and the integrand. Eventually I'd like to use it
inside a class, like the example below. I think the problem is that
in order to pass a member function, I have to qualify the name a bit
differently, for example:

typedef double (astro::universe::*pfn)(double);

(by the way, I don't think this actually worked...) But doing this
sort of thing of course doesn't make the integral class generic enough.

I'd like to get some advices as to how I can make such an integral
class that even a member function can be passed easily as the
integrand. How can you do this using C++?

Thanks in advance...

----------------------------------------------------------------

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

namespace math {

typedef double (*pfn)(double);

class integral
{
public:
integral(double a, double b, pfn f)
: lower_(a), upper_(b), integrand_(f)
{}

double lower_bound() const { return lower_; }
double upper_bound() const { return upper_; }
void change_bounds(double a, double b)
{
lower_ = a;
upper_ = b;
}
double simpson(unsigned int const n) const
{
double h = (upper_ - lower_) / n;
double sum = integrand_(lower_) * 0.5;
for (int i = 1; i < n; ++i)
sum += integrand_(lower_ + i * h);
sum += integrand_(upper_) * 0.5;

double summid = 0.0;
for (int i = 1; i <= n; ++i)
summid += integrand_(lower_ + (i - 0.5)*h);

return (sum + 2 * summid) * h / 3.0;
}

private:
double lower_;
double upper_;
pfn integrand_;
};

} // namespace math

namespace astro {

class universe
{
public:
universe(double const omega_matter,
double const omega_lambda,
double const hubble_const)
: omega_matter_(omega_matter),
omega_lambda_(omega_lambda),
hubble_const_(hubble_const)
{}

double comoving_distance(double z)
{
//
// Want to use a generic integral class above with
// the integrand astro::universe::inverse_e(double),
// but does not compile....
//
math::integral func(0.0, z, inverse_e);

double speed_of_light = 3.0e8;
double c_cgs = speed_of_light * (100.0);
double H0_cgs = hubble_const_ * (1.0 / 3.0856776e19);

return (c_cgs / H0_cgs) * func.simpson(1000);
}

private:
double hubble_const_;
double omega_matter_;
double omega_lambda_;

double inverse_e(double z)
{
return 1.0 / std::sqrt(omega_matter_ * std::pow(1 + z, 3)
+ omega_lambda_);
}
};

} // namespace astro

int main()
{
//
// Create a universe with a particular cosmology.
//
astro::universe uni(0.3, 0.7, 70.0);

//
// Compute a comoving distance to an object at redshift 0.4.
//
std::cout << uni.comoving_distance(0.4) << '\n';

return 0;
}
Jul 19 '05 #1
2 2450
WW
cupiemayo wrote:
Hello. I'm trying to make a sort of generic integral class, holding
the boundary values and the integrand. Eventually I'd like to use it
inside a class, like the example below. I think the problem is that
in order to pass a member function, I have to qualify the name a bit
differently, for example:

typedef double (astro::universe::*pfn)(double);

(by the way, I don't think this actually worked...) But doing this
sort of thing of course doesn't make the integral class generic
enough.

I'd like to get some advices as to how I can make such an integral
class that even a member function can be passed easily as the
integrand. How can you do this using C++?


Look at boost::function ( http://www.boost.org ). It might provide the
facility you are looking for. Might I say since you did not care to make
the smallest compilable code for your posts illiustration purposes, so I did
not care reading the code with my headache. :-(

--
WW aka Attila
Jul 19 '05 #2
cu*******@yahoo.com (cupiemayo) writes:
Hello. I'm trying to make a sort of generic integral class, holding
the boundary values and the integrand. Eventually I'd like to use it
inside a class, like the example below. I think the problem is that
in order to pass a member function, I have to qualify the name a bit
differently, for example:

typedef double (astro::universe::*pfn)(double);

(by the way, I don't think this actually worked...) But doing this
sort of thing of course doesn't make the integral class generic enough.
No, this won't work. The only solution I can think of is making
integral a templated class (see below).

I'd like to get some advices as to how I can make such an integral
class that even a member function can be passed easily as the
integrand. How can you do this using C++?

Thanks in advance...

----------------------------------------------------------------

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

namespace math {

typedef double (*pfn)(double);

class integral
{
public:
integral(double a, double b, pfn f)
: lower_(a), upper_(b), integrand_(f)
{}
The following should work (untested code):

template <class T>
class integral {
public:
typedef double (T::*pfn)(double);
integral(double a, double b, pfn f)
: lower_(a), upper_(b), integrand_(f) {}

...
namespace astro {

class universe
{
public:
universe(double const omega_matter,
double const omega_lambda,
double const hubble_const)
: omega_matter_(omega_matter),
omega_lambda_(omega_lambda),
hubble_const_(hubble_const)
{}

double comoving_distance(double z)
{
//
// Want to use a generic integral class above with
// the integrand astro::universe::inverse_e(double),
// but does not compile....
//
math::integral func(0.0, z, inverse_e);


math::integral<universe> func(0.0,z,inverse_e);

HTH & kind regards
frank

--
Frank Schmitt
4SC AG phone: +49 89 700763-0
e-mail: frankNO DOT SPAMschmitt AT 4sc DOT com
Jul 19 '05 #3

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

Similar topics

5
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,...
37
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...
7
by: jon wayne | last post by:
Hi I'm a little confused here about the lifetime of a static pointer to member function, Say, I declare,define & initialize a static ptr to mem function in the header file of a class(the class...
1
by: Daveyk0 | last post by:
Hello there, I have a front end database that I have recently made very many changes to to allow off-line use. I keep copies of the databases on my hard drive and link to them rather than the...
3
by: Lilith | last post by:
If I have a class object and a pointer to the object is there a performance hit for invoking a member function using member of (.) vs member of (->) due to pointer arithmetic? From an amateur...
3
by: dice | last post by:
Hi, In order to use an external api call that requires a function pointer I am currently creating static wrappers to call my objects functions. I want to re-jig this so I only need 1 static...
29
by: shuisheng | last post by:
Dear All, The problem of choosing pointer or reference is always confusing me. Would you please give me some suggestion on it. I appreciate your kind help. For example, I'd like to convert a...
6
by: smmk25 | last post by:
Before I state the problem, I just want to let the readers know, I am knew to C++\CLI and interop so please forgive any newbie questions. I have a huge C library which I want to be able to use in...
4
by: Immortal_Nephi | last post by:
I had a lot of research to see how function pointer works. Sometimes, programmers choose switch keyword and function in each case block can be called. Sometimes, they choose ordinary function...
2
myusernotyours
by: myusernotyours | last post by:
Hello everyone. I am using a C library from C++ code. The C library declares some function pointers using typedef, and then makes some variables using this function pointer as the type. Like: ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.