Solved

Pointer to a member function of an object.

Posted on 1998-12-23
12
227 Views
Last Modified: 2010-04-02
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
Comment
Question by:ramshakal
  • 3
  • 2
  • 2
  • +3
12 Comments
 
LVL 1

Expert Comment

by:The_Brain
ID: 1180923
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
 
LVL 1

Expert Comment

by:The_Brain
ID: 1180924
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
 
LVL 22

Expert Comment

by:nietod
ID: 1180925
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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 22

Expert Comment

by:nietod
ID: 1180926
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
 
LVL 1

Author Comment

by:ramshakal
ID: 1180927
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
 

Expert Comment

by:yoffe
ID: 1180928
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
 

Accepted Solution

by:
beki earned 100 total points
ID: 1180929
> 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
 

Expert Comment

by:yoffe
ID: 1180930
beki,
thx for paraphrasing my comment and submitting it as an answer.
0
 

Expert Comment

by:nbell
ID: 1180931
Perhaps this would be clearer if you explained why you can't accomplish this in a more concise manner through RTTI?
0
 

Expert Comment

by:beki
ID: 1180932
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
 
LVL 1

Author Comment

by:ramshakal
ID: 1180933
>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
 
LVL 1

Author Comment

by:ramshakal
ID: 1180934
>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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C++ - Loading Managed Assembly From Memory in Unmanaged Process 25 470
ASP.net build a IF/Then Walkthrough Guide 1 209
thread-safe code in c++ 2 100
C++ question 3 65
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question