473,695 Members | 2,416 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::univers e::*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(d ouble a, double b)
{
lower_ = a;
upper_ = b;
}
double simpson(unsigne d int const n) const
{
double h = (upper_ - lower_) / n;
double sum = integrand_(lowe r_) * 0.5;
for (int i = 1; i < n; ++i)
sum += integrand_(lowe r_ + i * h);
sum += integrand_(uppe r_) * 0.5;

double summid = 0.0;
for (int i = 1; i <= n; ++i)
summid += integrand_(lowe r_ + (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_(o mega_matter),
omega_lambda_(o mega_lambda),
hubble_const_(h ubble_const)
{}

double comoving_distan ce(double z)
{
//
// Want to use a generic integral class above with
// the integrand astro::universe ::inverse_e(dou ble),
// 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(10 00);
}

private:
double hubble_const_;
double omega_matter_;
double omega_lambda_;

double inverse_e(doubl e 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_di stance(0.4) << '\n';

return 0;
}
Jul 19 '05 #1
2 2463
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::univers e::*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::univers e::*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)(doubl e);
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_(o mega_matter),
omega_lambda_(o mega_lambda),
hubble_const_(h ubble_const)
{}

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


math::integral< universe> func(0.0,z,inve rse_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
9368
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);
37
4997
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.
7
3028
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 is a helper Singleton class, and is created & deleted as and when required) - Where does the static pointer point to when every single instance of the class is deleted. I presume it'll be dangling pointer- as the code seg to which it's
1
4008
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 live databases on the network. Is there a way, via code, when I get back in-house from being on the road to click a button, and select the backends I want to link to? I would want to delete all the current links and link to the "live"
3
1478
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 point of view I'd initially think that the pointer arithmetic would cause a slow down. On the other hand pointer arithmetic shouldn't even be an issue since the member functions aren't actually part of the object but instead a member of the class. ...
3
2615
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 wrapper function. I would prefer to be able to pass the member function as a void* to the static wrapper but I suspect this may not even be possible. Another solution would be option 2 below but I can't figure out the syntax to call obj->*pFn(). ...
29
3644
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 string to a integer number. bool Convert(const string& str, int* pData); bool Convert(const string& str, int& data);
6
7677
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 a .NET application and thus am looking into writing a managed C++ wrapper for in vs2005. Furthermore, this library has many callback hooks which need to be implemented by the C++ wrapper. These callback functions are declared as "extern C...
4
4397
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 pointer array instead of switch. They believe that ordinary function pointer is much faster than switch. Think of one variable called selectFunction. selectFunction variable can be the range of 0 through 1,000 or more. Load selectFunction...
2
2859
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: typedef void (*func) (int); //then the pointer... func ptr; For me to work with this library I need to assign one of my member functions to the library variable(the func pointer) above.
0
8574
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
8990
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8850
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,...
0
7664
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...
1
6493
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
5839
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
4342
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...
1
3007
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
3
1977
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.