• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 236
  • Last Modified:

Pointer to a member function of an object.

How can we store a pointer to the member function of a class. Do we have to first instantiate the class.

My problem is that i want to want to store function pointers in a structure member. For eg.,

struct A {
 ....
 void *ptr;
 ...
};

Different instances of A will be stored in a hash table along with a key. For each different class, there will be a row in the hash table containing an instance of class A. Depending upon the key used to store hash table, we will get an instance of structure A. Now, I want to store the address of a particular function (which is present in all classes) for each class.

Suggest me a way to do what I want to do. Incase my approach is incorrect, suggest an alternative approach.
0
ramshakal
Asked:
ramshakal
  • 3
  • 2
  • 2
  • +3
1 Solution
 
The_BrainCommented:
class A
{
  void* ptr();
}

this is a way,  if this is what you were looking for could you let me know:)
I wan't to leave it open for suggestion before I take the answer. (incase I am misunderstanding the question.)  thanks
0
 
The_BrainCommented:
besides, classes hold objects not struct.  (I think.;)
so the first change would be to Class and void *ptr
void* ptr();  (the position of the * doesn't matter, it is just more correct this way.  void *ptr(); can be misleading to understanding what the pointer ACTUALLY does.
0
 
nietodCommented:
struct A
{
   typedef int (A::*MthFuncPtr) (int,int);

   MthFuncPtr Ptr;
  int Add(int i, int j)  { return i + j };
  int Sub(int i, int j)  { return i - j };
}

A SomeA;

SomeA.Ptr = &A::Add;
int i = (A.Ptr.*Ptr)(5,10);

Let me know if you have questions.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
nietodCommented:
Hmmm.  You said
>> Now, I want to store the address of a particular function (which is
>> present in all classes) for each class.

I was assuming you meant the functions were present in the structure.  (Like I have above.)  Do you mean they are other classes/structures?  If so, then a pointer to member will not work.  For that you should use an ordinary pointer or preferably a functor.  

If the above is not want you need, a few more details might help.  (The above, by the way allows you to use a pointer to "choose" between multiple member function of a particular class, but not between member functions of multiple classes--that pretty much isn't allowed, but there are things that can be done.)

0
 
ramshakalAuthor Commented:
You are right. The functions will be of different classes. Let me explain the problem again. Suppose there are two classes, each having a common function say common(). Both these classes are derived from the same base class.

class S {
.
public:
virtual int common();
.
}

class A : public S {
 ....
public:
 int common();
.
};

class B : public S {
.
public:
int common()
.
};

Now we want to create a hash table which will have a (key, value) combination. The value will be a pointer to a structure. The structure will be of the following form :

struct  value {
 ...
void *ptr;
.
}

Now, I want to assign the address of the common function of A or B to the ptr, depending upon the key with which it is associated. So, when the hash table is made and somebody searches it using a particular key, he gets a value structure pointer whose ptr member will contain the address of the appropriate common function (of either A or B).

Is there a way by which this can be done.

0
 
yoffeCommented:
Just need a little clarification.

Does Common() operate on any of the member data?   If it doesn't, it should be made static (not virtual) and then you will have no problem storing the address of the member function.   If it does operate on member data, it is my understanding that you should be storing the address of the actual object is a S* element of your structure.   A call to
pS->Common() will resolve to the correct member function via the vfp table.

Am I being a bit myopic by this suggestion, or is there a really need to have the address directly stored in the structure.
0
 
bekiCommented:
> Now, I want to assign the address of the
> common function of A or B to the ptr,
> depending upon the key with which it is associated.

If the function is in a class, you do not want to return a pointer to it to the caller, because the pointer is only meaningful in combination with a pointer to an instance.

> So, when the hash table is made and somebody
> searches it using a particular key, he gets
> a value structure pointer whose ptr member
> will contain the address of the appropriate
> common function (of either A or B).

If this is what you want (is it?) then your hash table structure should contain pointers to the base class of A, i.e.:

struct  value {
 ...
S *ptr;
 
}

In this way, the caller can lookup the table, get an S* and then call S->common(). If the caller should be able to distinguish from whether the actual instance is A or B, you should either use RTTI or add another pure virtual to S that you can call to identify the instances.

Regards

Tomislav Trajkovski
Applify - The builder of Applify Business Site Server
http://applify.com

0
 
yoffeCommented:
beki,
thx for paraphrasing my comment and submitting it as an answer.
0
 
nbellCommented:
Perhaps this would be clearer if you explained why you can't accomplish this in a more concise manner through RTTI?
0
 
bekiCommented:
RTTI is always better if you are using the latest tools and libraries.

But if you happen to be using old development tools or an old class library that is/are not based on RTTI, you might have to trick RTTI yourself.
0
 
ramshakalAuthor Commented:
>If the function is in a class, you do not want to return a pointer to it to the caller, because the pointer is only
   > meaningful in combination with a pointer to an instance.
By making the Static member function we can have pointer without an instance.
So your explanation is in complete. In fact yoffe suggested the way of taking
the pointer to  static member func. But both of you took only one possibility.
0
 
ramshakalAuthor Commented:
>If the function is in a class, you do not want to return a pointer to it to the caller, because the pointer is only
   > meaningful in combination with a pointer to an instance.
By making the Static member function we can have pointer without an instance.
So your explanation is in complete. In fact yoffe suggested the way of taking
the pointer to  static member func. But both the answer were incomplete..
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 3
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now