Trying to built a program but meet some problem in it about the pointer to use.
I have created this -
double runge_kutta_4th(double(*equation)(double,double), double initial, double t, double dt){
-
double k1 = equation(initial, t);
-
double k2 = equation(initial + 0.5 * k1 * dt, t + 0.5 *dt);
-
double k3 = equation(initial + 0.5 * k2 * dt, t + 0.5 *dt);
-
double k4 = equation(initial + k3 * dt, t + dt);
-
return initial + (k1 + 2*k2 + 2*k3 + k4)/6 * dt;
-
}
-
which work fine with a function.
Now I try to implement it with a equation in a function
something like this - class equation{
-
public:
-
double a;
-
double b;
-
double c;
-
-
equation(double a, double b, double c):
-
a(a),
-
b(b),
-
c(c)
-
{
-
}
-
-
~equation(){
-
}
-
-
double equation1(double y, double t){
-
return (a) * t - b* y;
-
}
-
-
double equation2(double y, double t){
-
return 3 * (a) * t - b* y;
-
}
-
};
A bit stuck on how to pass the equation to the receiving function. Anyone have any idea? Might have to pass equation 1 on some time and pass equation to on another time. Or should i implement the solution in the class rather than calling it externally?
3 1965 Banfa 9,065
Expert Mod 8TB
If you want to pass you class to your function double runge_kutta_4th you need to define you class as a functor. That is a class with the operator() implemented.
You will also need to change the called function a bit. The following is an example of a function that can take a predicate that is either a function or a functor. -
#include <iostream>
-
#include <iomanip>
-
-
class MySquare
-
{
-
public:
-
MySquare(int init=1): multiple(init) {}
-
~MySquare() {}
-
-
int operator()(int x) const { return multiple * x * x; }
-
-
private:
-
int multiple;
-
};
-
-
template< typename PREDICATE >
-
void print(PREDICATE predicate, int input)
-
{
-
std::cout << std::setw(3) << input << ": " << predicate(input) << std::endl;
-
}
-
-
int Sum(int input)
-
{
-
int sum = 0;
-
-
for(;input > 0; input--)
-
{
-
sum += input;
-
}
-
-
return sum;
-
}
-
-
int main()
-
{
-
int ix;
-
-
for(ix=0; ix<10; ix++)
-
{
-
print(Sum, ix);
-
}
-
-
for(ix=0; ix<10; ix++)
-
{
-
print(MySquare(5), ix);
-
}
-
}
-
If you don't mind passing around an extra pointer, you can also declare a static method with the first parameter being a pointer or reference to your class. A static member still can access private data through a pointer/reference to it's class but the syntax is a little ugly. It goes like this: - class my_class {
-
public:
-
static int my_method(my_class* pthis)
-
{
-
return pthis->my_var;
-
}
-
private:
-
int my_var;
-
};
You could also use member function pointers like this: - class my_class {
-
void my_method(double a, double b) { ... }
-
};
-
-
void(class ::*func_ptr)(double, double);
-
func_ptr = &my_class::my_method;
-
-
my_class obj;
-
(obj.*func_ptr)(1.0, 2.0);
Thanks for both of the reply. I have make used of the functor in my code. Thanks for the example Banfa.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: David Hill |
last post by:
Hello -
I am using a library that takes a function pointer as an argument. Is the code below not possible?
int library_func(void (*func)(int, short, void *));
I am trying to do this...
...
|
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,...
|
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...
|
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...
|
by: keepyourstupidspam |
last post by:
Hi,
I want to pass a function pointer that is a class member.
This is the fn I want to pass the function pointer into:
int Scheduler::Add(const unsigned long timeout, void* pFunction, void*...
|
by: WaterWalk |
last post by:
Hello. I thought I understood member function pointers, but in fact I
don't. Consider the following example:
class Base
{
public:
virtual ~Base() {}
};
class Derived : public Base
{
|
by: Tim Frink |
last post by:
Hi,
I'm experimenting with function pointers and found
two questions. Let's assume this code:
1 #include <iostream>
2 class A;
3
4 ////////////////////////////////////////////
5 class B
|
by: Immortal Nephi |
last post by:
I would like to design an object using class. How can this class
contain 10 member functions. Put 10 member functions into member
function pointer array.
One member function uses switch to call...
|
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...
|
by: ghulands |
last post by:
I am having trouble implementing some function pointer stuff in c++
An object can register itself for many events
void addEventListener(CFObject *target, CFEventHandler callback,
uint8_t...
|
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
|
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...
|
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: 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...
|
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,...
|
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...
|
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,...
|
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: 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...
| |