Hi
I have problem at pointing to the function at the class structure.
To demonstrate my problem,I have written a simple code.
I have two function.
''Xsquared'' returns square of input value.
''multipleby4'' multiplies by 4 of input value.

The pointer to the Xsquared is the input value of the multipleby4.

#include <cmath>
#include <iostream>

using namespace std;

class Int
{
    public:
    Int( int);
    double xSquared(double x);
    double multipleby4(double (*F)(double));

    void CreateInt();
    private:
    int a;
    int result;
};
Int::Int( int b)
    :a(b)
{
    CreateInt();
}
double Int::xSquared(double x)
{
    // power function, can be replaced with x * x
    return pow(x, 2);
}
double Int::multipleby4(double (*F)(double))//Problem at this stage
    {
        double value = 4 * (*F)(a);
        return (value);
    }

void Int::CreateInt()
{
    result = multipleby4(xSquared);
}
int main()
{
Int ss(2);

return(0);
}

Recommended Answers

All 5 Replies

As far as I know (and I'll probably be corrected by Narue, Salem or ArkM), if you want to use function-pointers to member functions inside a class, that function has to be static.
Also your call is wrong : result = multipleby4(xSquared); should be result = multipleby4(&Int::xSquared); So then your code would look like this:

class Int
{
public:
    Int( int);
    static double xSquared(double);
    double multipleby4(double (*F)(double));
    void CreateInt();
private:
    int a;
    int result;
};

Int::Int( int b) :a(b)
{
    CreateInt();
}

double Int::xSquared(double x)
{
    // power function, can be replaced with x * x
    return pow(x, 2);
}

double Int::multipleby4(double (*F)(double))//Problem at this stage
{
    double value = 4 * (*F)(a);
    return (value);
}

void Int::CreateInt()
{
    result = multipleby4(&Int::xSquared);
}


int main()
{
    Int ss(2);
    return(0);
}

ps. I also threw your code through a formatter for readability. If you're using VS200x, you can just press crtl-a ctrl-k ctrl-f and your code is formatted. It reads a bit easier this way

But If I make my member function static ,it should not acceses non_static data member.And I dont want to make my data members static.
İs there any other way?

#include"stdafx.h"
#include <cmath>
#include <iostream>

using namespace std;


class Int
{
private:


	int a;
	
	int result;
public:
	typedef double (Int::*ptr)( double );

	Int( int);
	//must have default
	Int():a(){};
	double xSquared(double x);
	double multipleby4(double (Int::*ptr)( double ) );
	double multipleby4(double (*F)(double));

	void CreateInt();

};
Int::Int( int b)
:a(b)
{
	CreateInt();
}
double Int::xSquared(double x)
{
	// power function, can be replaced with x * x
	return pow(x, 2);
}
double Int::multipleby4(double (*F)(double))//Problem at this stage
{
	double value = 4 * (*F)(a);
	return (value);
}
// thats the way
Int i;
double d (double a)
{
	return i.xSquared(a);
}

void Int::CreateInt()
{
	result = multipleby4(d);
}


int _tmain(int argc, _TCHAR* argv[])
{
	Int ss(2);
	ss.CreateInt();

	return(0);
}

Honestly speaking, I can't understand your true problem. It seems no need in function pointers in your code at all. For example, xSquared member function does not bear a relation to the class Int, it does not use any class members. Evidently, it's an ordinar function: double sqr(double x) { return x*x; } (never use expensive pow function if you have the simplest expression ;)). Useful member functions of Int object like xSquared must have no arguments...

Pointers to member functions and pointers to ordinar functions are totally different beasts. Look at this code (it works ;)):

#include <cmath>
#include <iostream>

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

class Int
{
public:
    Int(int x = 0):i(x) {}

    double square() const { return (double)i*i; }
    double cube()   const { return square()*i; }
    double sqrt()   const { return ::sqrt((double)i); }

    double mult4(double (*f)(double x)) {
        return 4.0*f((double)i);
    }
    double mult4(double (Int::*f)()const) {
        return 4.0*(this->*f)();
    }
    operator int() const { return i; }
    Int& operator=(int x) { i = (x>0?x:0); return *this; }
private:
    int i;
};

const char eol('\n');

int main()
{
    Int two(2);
    std::cout 
        << two.mult4(square) << eol
        << two.mult4(&Int::square) << eol
        << two.mult4(&Int::cube) << eol
        << two.mult4(sqrt) << eol
        << two.mult4(&Int::sqrt)
        << std::endl;
    return 0;
}

See also:
http://www.parashift.com/c++-faq-lite/pointers-to-members.html
http://www.goingware.com/tips/member-pointers.html
http://www.newty.de/fpt/fpt.html

commented: I knew you would do that :P +12
commented: excellent link: goingware +5

Thank you ArkM ;
very useful links

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.