Solved

Pointer to methods

Posted on 2002-07-08
8
230 Views
Last Modified: 2010-04-01
Please, can anyone point me where can I find material about how do something like:

Object *pObject = new Object();

//This funcion store it in a proprietary table
pointer_store(pObject, &pObject->methodA())
{
...
}

//Execute from its table
exec_stored_pointer()
{
...
}
0
Comment
Question by:Korck
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 49

Expert Comment

by:DanRollins
ID: 7139260
You normally can't just directly jump to a method... the compiler has to set up the 'this' pointer.

There are several ways to do it, but the correct way FOR YOU will depend upon what you need to do.  Here is the summary:

1) You can save and excute a function that is a static member.  So when you declare the member function in the object header, use the keyword static (that causes some related problems in that a static member function can access only other static member functions and variables).

2) You can save the object pointer and use that to 'get into the object'

    //Execute from its table
    exec_stored_pointer() {
        pTheObject->(*pfnPonterThatWasStored)();
    }

3) You can 'be in the object' when you call the ptr to the stored fn:

CMyClass::DoThatThing( int nThingToDO )
{
    if (nTHingToDo==1) {
        int nRet= (*pFnThingToDoNumber1)();
    }
}

I will explain in detail if you will take a moment to describe the situation in which you will be using this technique.

-- Dan
0
 
LVL 30

Accepted Solution

by:
Axter earned 50 total points
ID: 7139411
The following method only works if all your methods have the same DEFAULT arguments.

#include <vector>
#include <algorithm>

#include <string> //Needed for example code
#include <iostream> //Needed for example code



class FunctionPointerBase
{
public:
     virtual void PerformFunction() = 0;
     virtual ~FunctionPointerBase(){}
};

template<typename T_obj, typename T_fx>
class FunctionPointer : public FunctionPointerBase
{
public:
    typedef T_fx T_func;
     FunctionPointer(T_obj &Obj, T_func Func): m_Obj(Obj), m_Func(Func){}
     virtual void PerformFunction()
     {
          (m_Obj.*m_Func)();
     }
     T_func m_Func;
     T_obj &m_Obj;
};

class DeleteObject
{
public:
     template<typename T>
          operator()(T& ptr)
     {
          delete &*ptr;
          ptr = NULL;
          return true;
     }
};

class FunctionPointerStore
{
public:
     template<typename T_obj, typename T_func>
     void pointer_store(T_obj &Obj, T_func Func)
     {
          Store.push_back(new FunctionPointer<T_obj,T_func>(Obj, Func));
     }
     void exec_stored_pointer(int Index)
     {
          Store[Index]->PerformFunction();
     }
     int GetSize() {return Store.size();}
     ~FunctionPointerStore()
     {
          Store.erase(std::remove_if(Store.begin(), Store.end(),DeleteObject()), Store.end());
     }
private:
     std::vector<FunctionPointerBase*> Store;
};

//Example classes
class foo
{
public:
     void SomeFunction()
     {
          std::cout << "foo" << std::endl;
     }
};

class foo2
{
public:
     void Xyz(void)
     {
          std::cout << "foo2" << std::endl;
     }
};

class wiget
{
public:
     void AnotherFuction(int x = 3)
     {
          std::cout << "wiget " << x  << std::endl;
     }
};

class wiget2
{
public:
     void Abc(std::string Data = "abc")
     {
          std::cout << "wiget2 " << Data << std::endl;
     }
};

//Example usage
int main(int argc, char* argv[])
{
     FunctionPointerStore My_FunctionPointerStore;
     foo My_foo;
     My_FunctionPointerStore.pointer_store(My_foo, foo::SomeFunction);
     wiget My_wiget;
     My_FunctionPointerStore.pointer_store(My_wiget, wiget::AnotherFuction);
     foo2 My_foo2;
     My_FunctionPointerStore.pointer_store(My_foo2, foo2::Xyz);
     wiget2 My_wiget2;
     My_FunctionPointerStore.pointer_store(My_wiget2, wiget2::Abc);
     My_FunctionPointerStore.exec_stored_pointer(0);
     My_FunctionPointerStore.exec_stored_pointer(1);
     My_FunctionPointerStore.exec_stored_pointer(2);
     My_FunctionPointerStore.exec_stored_pointer(3);
     return 0;
}
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7139446
Axter, It seems to me that that is a complicated way using my option #2.  If you have the object and a pointer to a function in that object, then you can call that function.

-- Dan

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Axter
ID: 7139456
>>Axter, It seems to me that that is a complicated way
>>using my option #2.  If you have the object and a
>>pointer to a function in that object, then you can call
>>that function.

That's because they're not the same thing.
Your option #2 works if all the objects are of the same type.  But will not work if objects of different types and different base types are being used.

In re-reading the question, your option #2 might be what the questioner is asking for.

When I first read it I thought the questioner is asking about storing different type objects.

Korck,
Are all the objects of the same type?
0
 

Author Comment

by:Korck
ID: 7139501
No...

I'm dooing a  configurable generic message dispatcher, so to do my events "configurable", I must be abble to unknow the method's object type.... and I don't want to allow only static methods and global functions.



0
 
LVL 30

Expert Comment

by:Axter
ID: 7139538
>>I'm dooing a  configurable generic message dispatcher,
>>so to do my events "configurable", I must be abble to
>>unknow the method's object type.... and I don't want to
>>allow only static methods and global functions.

So then the example I posted is inline with what you need.

Have you tried it out?
Do you understand it?
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7139768
>>I must be abble to unknow the method's object type....

I think you mean that when you call the function, you won't know what type of object you are calling.  It looks like Axter's code saves both an object reference and a pointer to a member function for that object.  So it will do the job.    

Of course the object must continue to exist from the time you save it to the time you use it's function.

MFC's message dispatcher uses a set of macros to build a similar table.  Another technique is to derive all of the objects from a particular base class and have the dispatch handler be a virutal function.

-- Dan
0
 

Author Comment

by:Korck
ID: 7141586
Yes, that´s it Axter.

Thank you all in advance :)

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

738 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