473,698 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::oper ator()(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 2293
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::oper ator()(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(FuncPt r 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,in t,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...@com Acast.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 objektorientier ter Datenverarbeitu ng
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...@gm ail.comwrote:
On Jul 4, 10:02 pm, "Victor Bazarov" <v.Abaza...@com Acast.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 objektorientier ter Datenverarbeitu ng
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::oper ator()(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<=10000000 00;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(squar e);
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
2376
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: void start(); void run();
6
1873
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 class Base) of the createInstance function below that produces the output: Output:
7
3029
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
6
2619
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
4406
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 same invocation from a DistantlyRelated class. What actually happened was that the compiler produced: error C2247: 'A::function' not accessible because 'CloselyRelated' uses 'private' to inherit from 'A' I'm guessing that the above compiler...
8
3345
by: nsharma78 | last post by:
Hi, I have a code as follows: class A { public: void print(){cout << "Magic" << endl;} };
6
1936
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 fixed class (33.2), any member of a fixed class (33.6), to a fixed function of any object (functiods, 33.10). What I wanted was to be able to hand any member function of any created object to a signal hander etc.
6
4152
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 tr1::functional to the first one, for example:
3
3182
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
2427
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 implmention. The problem is that non-member function cannot access private data member. The friend keyword is not the solution. I am aware that friend keyword allows the non-member function to access private data member. If you define more than...
0
8678
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
8609
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
9030
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
8899
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
8871
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7737
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
6525
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
4621
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
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

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.