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?