473,396 Members | 1,785 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.

Can you have a function pointer as a data member?

Can you have a function pointer as a data member? Like:

template <class T>
class Integrate
{
public:
Integrate(long double& (T* f)(T& x)):fn(f){}
long double& operator()(T& lowLimit, T& upLimit) const;
private:
long double& (T* fn)(T& x);
};

long double& Integrate::operator()(T& lowLimit, T& upLimit) const
{
long double sum = 0.0;
T x;
// Evaluate integral{a,b} f(x) dx
for (long long n=0;n<=100;n++)
{
x = (n/100.0)*(b-a)+a;
sum +=(*f)(x)*(b-a)/101.0;
}
return sum;
}

Thanks!!!

Jul 4 '07 #1
6 2279
On Wed, 04 Jul 2007 11:29:49 -0700, Protoman wrote:
Can you have a function pointer as a data member? Like:

template <class T>
class Integrate
{
public:
Integrate(long double& (T* f)(T& x)):fn(f){}
long double& operator()(T& lowLimit, T& upLimit) const;
private:
long double& (T* fn)(T& x);
};

long double& Integrate::operator()(T& lowLimit, T& upLimit) const
{
long double sum = 0.0;
T x;
// Evaluate integral{a,b} f(x) dx
for (long long n=0;n<=100;n++)
{
x = (n/100.0)*(b-a)+a;
sum +=(*f)(x)*(b-a)/101.0;
}
return sum;
}
Yes,

#include <iostream>

template <typename ReturnValue, typename Arg1, typename Arg2>
class Function
{
private:
typedef ReturnValue (*FuncPtr)(Arg1,Arg2);
FuncPtr d_fptr;
public:
Function(FuncPtr fptr) : d_fptr(fptr) {}
ReturnValue Call(Arg1 a1, Arg2 a2) {
return d_fptr(a1,a2);
}
};

int plus(int a, int b) {
return a + b;
}

int main() {
Function<int,int,intPlus(&plus);
std::cout<<Plus.Call(2,1)<<std::endl;
return 0;
}

--
Obnoxious User
Jul 4 '07 #2
Protoman wrote:
Can you have a function pointer as a data member?
You can have any type as data member.
Jul 4 '07 #3
"Rolf Magnus" <ra******@t-online.dewrote...
Protoman wrote:
Can you have a function pointer as a data member?

You can have any type as data member.

... except 'void', of course. Not sure if we consider it a type,
however. Also, an incomplete type is not allowed.

V
--
Please remove capital A's from my address when replying by mail
Jul 4 '07 #4
Victor Bazarov wrote:
"Rolf Magnus" <ra******@t-online.dewrote...
>Protoman wrote:
Can you have a function pointer as a data member?

You can have any type as data member.


.. except 'void', of course. Not sure if we consider it a type,
however. Also, an incomplete type is not allowed.
When I clicked the send button, I knew there was something I was missing ;-)
Btw: IIRC, void is an incomplete type.

Jul 4 '07 #5
On Jul 4, 10:02 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
"Rolf Magnus" <ramag...@t-online.dewrote...
Protoman wrote:
Can you have a function pointer as a data member?
You can have any type as data member.
.. except 'void', of course. Not sure if we consider it a type,
however. Also, an incomplete type is not allowed.
Void is a type. An incomplete type. All members must be
complete.

Also, a data member can't have a function type, otherwise, it
would be a function member:-).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 5 '07 #6
On 5 Jul, 02:06, James Kanze <james.ka...@gmail.comwrote:
On Jul 4, 10:02 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
"Rolf Magnus" <ramag...@t-online.dewrote...
Protoman wrote:
Can you have a function pointer as a data member?
You can have any type as data member.
.. except 'void', of course. Not sure if we consider it a type,
however. Also, an incomplete type is not allowed.

Void is a type. An incomplete type. All members must be
complete.

Also, a data member can't have a function type, otherwise, it
would be a function member:-).

--
James Kanze (GABI Software) email:james.ka...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Oh, and is this an efficient way to solve my problem of integrating an
explicit function in x:

#include <iostream>
using namespace std;

class Integrate
{
public:
Integrate(long double (*f)(long double& x)):fn(f){}
long double operator()(long double& a, long double& b)const;
private:
long double (*fn)(long double& x);
};

long double Integrate::operator()(long double& a, long double&
b)const
{
long double sum=0.0;
// Evaluate integral{a,b} f(x) dx
for (long long n=0;n<=1000000000;n++)
{
long double x = (n/1000000000.0)*(b-a)+a;
sum +=(*fn)(x)*(b-a)/1000000001;
}
return sum;
}

long double square(long double& x);

int main()
{
long double a,b;
cout << "Enter a lower and upper bound: ";
cin >a >b;
Integrate integrate(square);
cout << "The integral of x^2 from " << a << " to " << b << " is " <<
integrate(a,b);
return 0;
}

long double square(long double& x)
{
return (x*x);
}

How do you think I could do this at compile-time w/ template
metaprogramming?

Jul 5 '07 #7

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

Similar topics

4
by: Morgan Cheng | last post by:
I tried to build a CThread on linux which imitate the behavior of java Thread. I think this is still in the scope of C++. First, I tried this way #include <pthread.h> class CThread { public:...
6
by: Querejeto | last post by:
Hello: Is it possible to detect programmatically the constness of a member function when it is called? That is, I would like to see a generic implementation (i.e. it does not depend on the...
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...
6
by: marco_segurini | last post by:
Hi, the following sample code shows a compiler error I get trying to build some old code with the last CL compiler (vers 13.10.3077): //----- begin #include <iostream> namespace ns {
6
by: Bill Rubin | last post by:
The following code snippet shows that VC++ 7.1 correctly compiles a static member function invocation from an Unrelated class, since this static member function is public. I expected to compile the...
8
by: nsharma78 | last post by:
Hi, I have a code as follows: class A { public: void print(){cout << "Magic" << endl;} };
6
by: joosteto | last post by:
Subject: pointer to any member function of any class. The "C++ FAQ Lite" explains how to hand a pointer to a member function to a signal handler etc, but only to a static function (33.2), to a...
6
by: Caleb | last post by:
I have a class that has two member functions of the same name: class MyClass { public: void someFunction(int arg); void someFunction(int arg, char blah); }; I'm trying to get a...
3
by: Dmitry | last post by:
Hi all, Consider the following code: class A { public: A() {} void DoMethod() { (this->*m_pMethod)(); } protected: virtual void MethodA() { ... }
2
by: Immortal Nephi | last post by:
I design a class for general purpose. I do not allow a client to read or modify interface and implemention. I allow them to write a new non- member function outside of class' interface and...
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:
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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
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,...
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...

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.