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: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);
};
};
};
};
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: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);
};
};
};
};