Solved

Pointer to a member function of an object.

Posted on 1998-12-23
12
226 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
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now