instance based callbacks

J

johny smith

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.
 
J

John Harrison

johny smith said:
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
 
D

David White

johny smith said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top