473,395 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

instance based callbacks

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
2 1641

"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Melissa Wallis | last post by:
I have a class with 5 callbacks. Two of the callbacks work fine but the others don't. The main difference is that the callbacks that don't work are composed of a sequence of structs. I noticed a...
31
by: Brian Sabbey | last post by:
Here is a pre-PEP for what I call "suite-based keyword arguments". The mechanism described here is intended to act as a complement to thunks. Please let me know what you think. Suite-Based...
30
by: Joost Ronkes Agerbeek | last post by:
Why is it allowed in C++ to call a static member function of an object through an instance of that object? Is it just convenience? tia, Joost Ronkes Agerbeek
5
by: Christopher Jastram | last post by:
I'm a self-taught programmer, so this might be a pretty dumb question. If it is, please point me in the right direction and I shall apologize profusely. I have a question regarding C++ and...
8
by: promko | last post by:
Hi! I need to call the following unmanged method: HFCI __cdecl FCICreate( void FAR* pv) I have written the next managed declaration: public static extern IntPtr FCICreate( IntPtr pv );
5
by: Edwin Knoppert | last post by:
If these are ActiveX based, that would not make my day. We have the strong believe trying never to force a user to enable ActiveX. (Like stupid updates from mcafee enforce to the use of activex...
2
by: tony | last post by:
Hi! A derived class can override a method in the base class it inherits for, and even my dog knows that. More incredibly, I know and understand it too. But, can a (of course virtual in this...
6
by: HolyShea | last post by:
All, Not sure if this is possible or not - I've created a class which performs an asynchronous operation and provides notification when the operation is complete. I'd like the notification to be...
9
by: Dahak | last post by:
I'm trying to generate dynamic functions to use as separate callbacks for an AJAX API call. The API doesn't seem to allow for the inclusion of any parameters in the callback, so I can't...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.