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. 2 1660
"johny smith" <pr************ **@charter.net> wrote in message
news:10******** *****@corp.supe rnews.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::startC ar);
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
"johny smith" <pr************ **@charter.net> wrote in message
news:10******** *****@corp.supe rnews.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::*pStar tFn)())
: mEngine(engine) , mpStartFn(pStar tFn) {}
void Start()
{
(mEngine.*mpSta rtFn)();
}
private:
Engine &mEngine;
void (Engine::*mpSta rtFn)();
};
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 comment about this same
problem on the web but no solution was noted. What makes the callbacks with
the sequences so different? It seems that when one of the callbacks with a
sequence is called it just hangs. I am talking to a TAO orb from...
|
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 Keyword Arguments
-----------------------------
Passing complicated arguments to functions is currently awkward in Python.
For example, the typical way to define a class property winds up polluting
the class's namespace with the property's get/set...
|
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
|
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 object members. Can anyone help?
I'm writing a C++ wrapper for a fairly old programming interface to a
document editing program that has no OOP whatsoever; only tons of
structs. This program has different callbacks I'm supposed to
implement for...
|
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
);
| |
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 for example)
|
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 case) method be overriden in an
instance instead? For example, to provide some callbacks to a class.
Should I use virtual functions instead? How do I pass the this pointer
when casting it from or to in any useful way seems to break every C++
|
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 performed on
the same thread thread that instantiated the class. One way to do this
is to pass an ISynchronizeInvoke into the class and use it to
synchronize the callback. In the constructor of the class, could I
take note of the current thread...
|
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 associate the call with the handling
routine (for reference purposes, I'm calling the Google Language
Translation API). As a work-around, I thought I'd dynamically
generate a unique callback function for each API call.
Right now, I'm stuck hobbling...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |