dimanche 11 avril 2021

C++ How to register a function to receive events [duplicate]

I'm new to C++ so I'm not sure what they called to what I'm trying to achieve making it hard for me to solve this.

I like to create a class that will notify a function when a new result has been done. It's something like a function that waits until something has been done. I'm not sure if this is possible, but here's my pseudo code.

ExampleClass.h

#pragma once
#ifndef EXAMPLECLASS_H
#define EXAMPLECLASS_H
#include "Header.h"

class ExampleClass
{

public:
    ExampleClass();
    ~ExampleClass();
    void Sum(int a, int b);
    
private:
    int a, b;
};

#endif

ExampleClass.cpp

#include "ExampleClass.h"

ExampleClass::ExampleClass() {
    a = 0;
    b = 0;
}

void ExampleClass::Sum(int A, int B) {
    a = A;
    b = B;
    
    ...
    
    int sum = a + b;
    //notify the `CallThisFunction` function about the result.
}

ExampleClass::~ExampleClass() {
    ...
}

then, for example in my main, I have the following.

void CallThisFunction(int sum){ // can this be called from ExampleClass?
    // do something here...
}


int main(){

    ExampleClass exampleClass1, exampleClass2;
    ...
    exampleClass1.Sum(1,1);
    exampleClass2.Sum(2,2);
    
}

anyone can help me?

Aucun commentaire:

Enregistrer un commentaire