By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,389 Members | 1,881 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,389 IT Pros & Developers. It's quick & easy.

instance based callbacks

P: n/a
I cannot seem to figure out how to do instance based callbacks for some
reason. I found one site on functors but I did not find it that helpful
actually I just don't understand it.

I have no problem doing non-instance based callbacks.

I want to be able to pass a function into a constructor and from inside the
class make a call make a call to the instance based function of another
class. Do i have to use templates?

so,
class Car {
Car( // instance based function ) {} // pass the instance based
function in the constructor

startCar() {
// make a call to Start Car in the Engine Class that is make a
call to another classes function which is not static.
}
};

class Engine {

Engine(){}

startCar() { std::cout << "started car" << std::endl; } // this function
is called from another class, this is not static.
};
int main()
{

Car car1( // pass instance based function ) // create a constructor and
pass the instance based function in

return 0;
}

Sorry if this is confusing, any guidance is greatly appreciated.
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"johny smith" <pr**************@charter.net> wrote in message
news:10*************@corp.supernews.com...
I cannot seem to figure out how to do instance based callbacks for some
reason. I found one site on functors but I did not find it that helpful
actually I just don't understand it.

I have no problem doing non-instance based callbacks.

I want to be able to pass a function into a constructor and from inside the class make a call make a call to the instance based function of another
class. Do i have to use templates?

so,
class Car {
Car( // instance based function ) {} // pass the instance based
function in the constructor

startCar() {
// make a call to Start Car in the Engine Class that is make a
call to another classes function which is not static.
}
};

class Engine {

Engine(){}

startCar() { std::cout << "started car" << std::endl; } // this function is called from another class, this is not static.
};
int main()
{

Car car1( // pass instance based function ) // create a constructor and
pass the instance based function in

return 0;
}

Sorry if this is confusing, any guidance is greatly appreciated.


No you don't have to use templates or functors, although both are often used
in this context.

I think the point you are missing is that you not only have to pass in the
instance based function (never heard that terminology before, although I
like it) but also the instance itself. Here's a quick example

class Engine;

class Car
{
public:
Car(Engine* e, void (Engine::*s)()) : engine(e), starter(s) {}
void startCar()
{
(engine->*starter)();
}
private:
Engine* engine;
void (Engine::* starter)();
};

class Engine
{
public:
void startCar() { cout << "car started\n"; }
};

int main()
{
Engine e;
Car c(&e, &Engine::startCar);
c.startCar();
}

I haven't compiled or tested this, so apologies for any typos.

Also worth pointing out that this would normally be done with virtual
functions not callbacks.

john
Jul 22 '05 #2

P: n/a
"johny smith" <pr**************@charter.net> wrote in message
news:10*************@corp.supernews.com...
I cannot seem to figure out how to do instance based callbacks for some
reason. I found one site on functors but I did not find it that helpful
actually I just don't understand it.

I have no problem doing non-instance based callbacks.

I want to be able to pass a function into a constructor and from inside the class make a call make a call to the instance based function of another
class. Do i have to use templates?
No.

so,
class Car {
Car( // instance based function ) {} // pass the instance based
function in the constructor

startCar() {
// make a call to Start Car in the Engine Class that is make a
call to another classes function which is not static.
}
};

class Engine {

Engine(){}

startCar() { std::cout << "started car" << std::endl; } // this function is called from another class, this is not static.
};
int main()
{

Car car1( // pass instance based function ) // create a constructor and
pass the instance based function in

return 0;
}

Sorry if this is confusing, any guidance is greatly appreciated.


Yes, it is confusing. I can't tell from your description what's calling
what, but I'll give an example with Car calling an Engine function through a
pointer, and you can take it from there.

class Engine
{
public:
void start();
};

class Car
{
public:
Car(Engine &engine, void (Engine::*pStartFn)())
: mEngine(engine), mpStartFn(pStartFn) {}
void Start()
{
(mEngine.*mpStartFn)();
}
private:
Engine &mEngine;
void (Engine::*mpStartFn)();
};

int main()
{
Engine e;
Car c(e, &Engine::start);
c.Start();
}

It's hard to see the purpose of the pointer, though. Normally there'd only
be one way to start a car or an engine, so why make it variable?

DW

Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.