473,407 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

A couple questions about function pointers in a class

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][b] = 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][b];};
virtual void input(std::istream& fin = std::cin )
{
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
fin >p[i][j];
};
};
};
virtual void output(std::ostream& fout = std::cout ) const
{
for(int i = 0; i < m; i++)
{
fout << "[\t";
for(int j = 0; j < n; j++)
{
fout << p[i][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[i][j],i,j);
};
};
};
};

Apr 11 '07 #1
1 1870

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][b] = 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][b];};
virtual void input(std::istream& fin = std::cin )
{
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
fin >p[i][j];
};
};
};
virtual void output(std::ostream& fout = std::cout ) const
{
for(int i = 0; i < m; i++)
{
fout << "[\t";
for(int j = 0; j < n; j++)
{
fout << p[i][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[i][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);
}

Apr 11 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: CoolPint | last post by:
As a self-exercise, I am trying to write a generic Priority Queue, which would store any type and and accept any user-definable "priority" function. After much tinkering, I came up with...
2
by: N3TB1N | last post by:
Let me try again. I could use some help with this assignment, even though my teacher does not grade assignments.but because I need to know this stuff for a test very soon, but haven't been in...
8
by: ctick | last post by:
In C++ FAQ 20.5, "...here are the mechanical details of why you need a virtual destructor when someone says delete using a Base pointer that's pointing at a Derived object. When you say delete p,...
71
by: cj | last post by:
Dear friends, I have one more questions for everyone in the newsgroup: I am preparing for an interview on UNIX/C++. Could you please identify some of the most important questions which might be...
3
by: Markus Dehmann | last post by:
I have a class "Data" and I store Data pointers in an STL set. But I have millions of inserts and many more lookups, and my profiler found that they cost a lot of runtime. Therefore, I want to...
7
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ...
39
by: Digital Puer | last post by:
I'm not the world's greatest C++ programmer, so I had a hard time with these. Some help would be appreciated. 1. Comment on the declaration of function Bar() below: class Foo { static int...
10
by: weird0 | last post by:
I heard of two c# interview questions that i still clearly don't know and havent understood the concept. One reason is also that I havent worked upon them 1. What is the difference between a...
7
by: tradevol | last post by:
as this example, question: If my purpose is initialize data from xml files and store them in the vector, so they can be used in class B by other member functions, do you think functionP is a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
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...
0
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,...
0
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...

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.