![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 243
Rep Power: 3
![]() |
Pointers to functions question
Is there any way I can create a pointer to an arbitrary function (whose arguments need not be specified?
Purpose: OpenGL. I have a class in which want to be able to define the material properies, and then associate the void pointer of that class to a function that draws the object. But I want to be able to use any function available, like glutDrawTorus or a custom function made by me. class test{
class test {
public:
float *materialProperty1;
float *materialProperty2;
float *materialProperty3;
void (*functionToCall)();
void setFunction( void (*func)() )
{
functionToCall = func;
}
};That is the general idea, but that won't work if the function I am associating to funcToCall has any number of arguments. Is there any way I can bypass this limitation?
__________________
Project::Soulstorm (personal homepage) |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I pretty sure you can't do that in C++ because of type safety issues. You might have a look at boost::function to see if you can work something out.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 243
Rep Power: 3
![]() |
Nah. Boost might help but I really don't want to jump into learning Boost, since I recently began with OpenGL (again)... Thanks, anyway.
__________________
Project::Soulstorm (personal homepage) |
|
|
|
|
|
#4 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4
![]() |
I came up with something like this. It's not pretty, and it might not do what you want, but I think it's pretty close.
![]() #include <iostream>
// Optionally, FunctionA
// could have some internal data that
// helps compute the results of a function call.
// For example, a table lookup functor may store
// a table.
struct FunctionA {
FunctionA() {}
// this one will take an int
void call( int n ) {
// example
std::cout << n * n << std::endl;
}
};
struct FunctionB {
FunctionB() {}
// this one will take 2 floats
void call( float a, float b ) {
std::cout << a * b << std::endl;
}
};
template <typename FunctionType>
class Test {
private:
// mutable because the function might want to
// modify its internal state when called.
mutable FunctionType fun_;
public:
void setFunction( const FunctionType &function ) {
fun_ = function;
}
FunctionType &action() { return fun_; }
};
int main() {
Test<FunctionA> testa;
testa.setFunction( FunctionA() );
testa.action().call( 3 );
Test<FunctionB> testb;
testb.setFunction( FunctionB() );
testb.action().call( 4.2, 3.6 );
}The disadvantages I can think of are that you would have to wrap every function in a struct and wouldn't be able to change the function type associated with a class during runtime. You could maybe get around that by writing a copy constructor for Test and when you need to change the function, do something like: Test<FunctionB> tester( tester ); // Where tester is Test<FunctionA> I don't really know if that is even possible though. :p
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! Last edited by Jessehk; Sep 23rd, 2007 at 10:15 AM. |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Jun 2006
Location: Ireland
Posts: 152
Rep Power: 3
![]() |
I know you've already dismissed Boost, but bind from the Boost libraries will allow you to do that. You would be able to use it without learning any other boost libraries.
__________________
Visit my website BinaryNotions. |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Mar 2007
Posts: 24
Rep Power: 0
![]() |
You can use boost::bind.
|
|
|
|
|
|
#7 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4
![]() |
In case Soulstorm doesn't respond, could one of you post an example of what he is describing using Boost.Bind? I'd be really curious.
![]()
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
#8 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
You might want to look up the notion of "functors" (or "function objects"), as these may provide a solution to the original problem. Practically, a functor may be implemented as a variation of Jessehk's approach, but supplying overloaded versions of operator() that accept the required argument types.
Incidentally, the mathematical notion of a functor (a mapping between categories) is formally related to the notion of a function object support by some languages, but the theory may be a little daunting if you try to use that as a starting point. |
|
|
|
|
|
#9 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 243
Rep Power: 3
![]() |
I will look at Jessehk's approach at some point later today. Although I doubt if it will prove to be useful for the things I want it to. It tends to do things more complicated if used with OpenGL functions.
Boost::bind may solve the problem, but I want to have as a solution something that doesn't require the programmer to have the boost libraries installed in his/her machine.
__________________
Project::Soulstorm (personal homepage) |
|
|
|
|
|
#10 |
|
Professional Programmer
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3
![]() |
Boost.Bind and Boost.Function libraries are header only. You can copy them out of the boost distribution directly into your project. This is allowed and encouraged.
For the complicated use cases there is even a tool which will pull all the other libraries a boost library depends on out as well (Not needed for boost.bind and boost.function). I can't remember the tool off the top of my head though.
__________________
Robotics @ Maryland AUV Team - Software Lead |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| C# corruption!!! | Kilo | C++ | 32 | May 21st, 2006 8:44 PM |
| Question about passing objects into member functions | aznluvsmc | C++ | 7 | Sep 27th, 2005 11:41 PM |
| Pointers in C (Part II) | Stack Overflow | C | 2 | Apr 29th, 2005 10:39 AM |
| Pointers in C (Part I) | Stack Overflow | C | 4 | Apr 28th, 2005 7:03 PM |
| User-defined creatNode and deleteNode functions for a doubly-linked list | jgs | C | 2 | Apr 28th, 2005 8:53 AM |