A couple questions about function pointers in a class

B

Bushido Hacks

A private utility function using a function pointer sounds ideal to
me. I want to simpify writing the same set of loops. While there are
a few functions that can't use it, setting and modifying values sounds
ideal.

I would like to know if the usage of this function pointer is valid
before I refactor some of the other functions that use the same data
structure to complete functions.

class Object
{
public:
/* these functions won't be posted in this message */
Object(int = 1, int = 1);
Object(const int, const int, const double** );
Object(const Object& );
virtual ~Object();
Object& operator=(const Object& );
virtual Object* clone();
// ...
Object& setVal(double v,int a,int b) { p[a] = v; return *this;};
Object& setVal(double** p){setAll(&setVal,&p); return *this;}; //
using the function pointer
// ...
virtual double** getVal() const { return p;};
virtual double getVal(int a, int b) const { return p[a];};
virtual void input(std::istream& fin = std::cin )
{
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
fin >> p[j];
};
};
};
virtual void output(std::eek:stream& fout = std::cout ) const
{
for(int i = 0; i < m; i++)
{
fout << "[\t";
for(int j = 0; j < n; j++)
{
fout << p[j] << "\t";
};
fout << "]\n";
};
};
protected:
int m; // number of rows
int n; // number of columns
double** p; // matrix values
private:
typedef Object& (*setFunc)(double,int,int);
typedef double (*getFunc)(int,int) const;
Object& setAll(setFunc sf, double** p)
{
for(int i = 0; i < m; i++)
{
for(int j = 0; j < m: j++)
{
sf(p[j],i,j);
};
};
};
};
 
O

Ondra Holub

Bushido Hacks napsal:
A private utility function using a function pointer sounds ideal to
me. I want to simpify writing the same set of loops. While there are
a few functions that can't use it, setting and modifying values sounds
ideal.

I would like to know if the usage of this function pointer is valid
before I refactor some of the other functions that use the same data
structure to complete functions.

class Object
{
public:
/* these functions won't be posted in this message */
Object(int = 1, int = 1);
Object(const int, const int, const double** );
Object(const Object& );
virtual ~Object();
Object& operator=(const Object& );
virtual Object* clone();
// ...
Object& setVal(double v,int a,int b) { p[a] = v; return *this;};
Object& setVal(double** p){setAll(&setVal,&p); return *this;}; //
using the function pointer
// ...
virtual double** getVal() const { return p;};
virtual double getVal(int a, int b) const { return p[a];};
virtual void input(std::istream& fin = std::cin )
{
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
fin >> p[j];
};
};
};
virtual void output(std::eek:stream& fout = std::cout ) const
{
for(int i = 0; i < m; i++)
{
fout << "[\t";
for(int j = 0; j < n; j++)
{
fout << p[j] << "\t";
};
fout << "]\n";
};
};
protected:
int m; // number of rows
int n; // number of columns
double** p; // matrix values
private:
typedef Object& (*setFunc)(double,int,int);
typedef double (*getFunc)(int,int) const;
Object& setAll(setFunc sf, double** p)
{
for(int i = 0; i < m; i++)
{
for(int j = 0; j < m: j++)
{
sf(p[j],i,j);
};
};
};
};


Hi.

typedef double (*getFunc)(int,int) const; is wrong. const qualifier
can be used only for (non-static) class methods.

You should post minimized, but compilable piece of code (or in case of
compilation problems minimized code which does not compile).

Methods should not be finished with semicolon like in
virtual double** getVal() const
{
return p;
};

You may not assign pointer to (non-static) class method to ordinary
function pointer. It is something different, because for using pointer
to class method, you need instance of class too.

I would suggest to not to use function pointers. You can write simple
abstract interface like
class SomeInterface
{
public:
virtual void DoSomething(int, double) = 0;
};

And then pass reference to this interface as parameter to some method:
class SomeClass
{
public:
// Constructors and destructors omitted for simplicity

void Method(SomeInterface& interface)
{
interface.DoSomething(int_data_, double_data_);
}

private:
int int_data_;
double double_data_;
};

If you really need function pointers, you can pass pointers to static
class methods (they act the same way as global functions in different
namespace) or pointers to ordinary functions.

If you really need to use pointers to class methods (and you usualy do
not need it), you have to use operators ->* and .* as shown on
following example (it does not anything usefull, it is just for
demonstration).

#include <iostream>

class A
{
public:
typedef
void (A::*MethodPtr)(int) const;

A(MethodPtr method_ptr)
: method_ptr_(method_ptr)
{
}

void Call(int data)
{
(this->*method_ptr_)(data);
}

void Method1(int data) const
{
std::cout << "A::Method1(" << data << ")\n";
}

void Method2(int data) const
{
std::cout << "A::Method2(" << data << ")\n";
}

private:
MethodPtr method_ptr_;
};

int main()
{
A a1(&A::Method1);
a1.Call(10);
a1.Call(20);

A a2(&A::Method2);
a2.Call(30);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,274
Latest member
JessMcMast

Latest Threads

Top