Skip to main content
GameDev.net gamedev.net
🔒 Locked

C++ Question

Started by nickvbs Feb 21, 2003 at 3:07 PM 2 replies 200+ views
Original Post
nickvbs
nickvbs
I was wondering if theres a way you can write a piece of code and store it in a structure/class as a function so that each instance of the structure/class has a different function depending on what code u stored in it..I dont know, maybe my thinking process is messed up.lol. Also how could I make a function parameter be another function, for example, the qsort function lets u pass in a compare function as the 4th parameter.
civguy
civguy
www.function-pointer.org

Just read on from the introduction, it answers your questions
falkone
falkone
I don''t think the qsort function takes a compare function.. i think it takes the result of the compare function... the only way to pass a function i think would be to use a function pointer..

as for a piece of code that does whatever.. i can''t make out what you''re trying to ask
Disclaimer: "I am in no way qualified to present advice n any topic concerning anything and can not be held responsible for any damages that my advice may incurr (due to neither my negligence nor yours)" o
fizban75
fizban75
Simple:

  
struct functor {
void operator()(int x) {}
};

// use:


class A {
public:
void call_func(int x) { m_func(x); }

private:
functor m_func;
};

By use of the operator() method, you can make a class look like a function. Then you can create multiple instances of that "functor" class and store it in different places. That help?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.