0

I've been looking for a few days and at this point I assume that I'm not looking with the right criteria.Here's what I need to do

class AB
{
    public:
        std::function<void()>addr;
};

class BC
{
    public:
        void myTestFunc(std::string value)
        {
            std::cout << "AB::myTestFunc called: " << value << std::endl;
        }
};

int main(void)
{
    AB ob;
    BC obj2;

    ob.addr = std::bind(&obj2::myTestFunc, "");
    // so that i can call
    (ob.addr)("testing");

    return 0;
}

The problem is that I need to have a class that can allow instances of other classes its used to call a function on the "parent" or main class. The actual arguments and functions are obviously different but I cannot get it to work with a class public function. I cannot make it static as each instance of the "child" class will be calling a separate function. Please tell me what I should be searching for and / or what I'm doing wrong.

YatShan
  • 425
  • 2
  • 8
  • 22
  • Have you looked at a [friend declaration](https://en.cppreference.com/w/cpp/language/friend)? See also example with overloading `<<` and `>>` for class use in [Can't load correct information from file](https://stackoverflow.com/questions/60245777/cant-load-correct-information-from-file) – David C. Rankin Feb 18 '20 at 22:08
  • You have a ways to go with understanding the difference between a blueprint, an object, and addresses of functions vs addresses of classes. Classes, as objects, don't contain their functions, because the function is universal. I will answer with your stuff rewrote – Stephen J Feb 18 '20 at 22:17
  • Basically, you need to pay attention to your types. Get more familiar with reading Generics out loud. You had a function pointer for functions of void() type... so fixing that will take care of rest – Stephen J Feb 18 '20 at 22:19

1 Answers1

1

I believe this is where you're going.

class AB
{
    public:
        std::function<void(std::string)>addr;
};

class BC
{
    public:
        void myTestFunc(std::string value)
        {
            std::cout << "AB::myTestFunc called: " << value << std::endl;
        }
};

int main(void)
{
    AB ob;
    BC ob2;
    std::string i = "testing";
    ob.addr = std::bind(&BC::myTestFunc, &ob2, i);
    ob.addr(i);

    return 0;
}
Stephen J
  • 2,367
  • 2
  • 25
  • 31
  • Thank you for the reply but no. as I stated it cannot be static as there will be several instances of the classes being used and each will need to be able to have its own callback and I don't want to force inheritance for each and every use of it. – James Wilson Feb 19 '20 at 00:30
  • 1
    Ok. Let's go over what functions are... basically they're addresses in memory to where instructions reside. CS50 by Malan is a great course you can watch free anywhere with vids. I believe if you want to explain the architecture of why/what you need, we can find a solution – Stephen J Feb 19 '20 at 00:31
  • here's a bit more elaboration as to what i'm trying to figure out. if i had a class Window and that Window has a button from Button class, i need to be able to have the window class specify a function for the button to call when clicked. for example window::wasclicked() and button->setCallback(&window::wasclicked) or similar type setup the problem lies in the fact that the name cant be known until runtime. – James Wilson Feb 19 '20 at 00:32
  • 1
    Ah ok. I haven't done C++ in awhile so I was off a tad. First, the vocabulary you're after is member function. I found an example. But also, you may want to look for examples of polling vs event driven ui. Those are the keywords you're looking for. https://www.tutorialspoint.com/function-pointer-to-member-function-in-cplusplus That link should help. – Stephen J Feb 19 '20 at 00:47
  • thanks, i did actually review that (see how my example code is similar lol) it breaks down when the function to call is within its own class and thats where i got stuck lol. you've been a good resource Stephen. thank you for your input – James Wilson Feb 19 '20 at 00:50
  • 1
    oh man, I did think it was similar. I'll try to rewrite it a bit, maybe I'll get it... lol. I'm used to raw function pointers. Usually functionName is the func ptr and functionName() is calling it. But that's in a raw pointer, not a member func. – Stephen J Feb 19 '20 at 00:56
  • Figured it out, tho a day later. Editing above with the below question. Might mark as dupe. But you seemed intelligent and beginning so I figured you'd do better with an answer. https://stackoverflow.com/questions/37636373/how-stdbind-works-with-member-functions – Stephen J Feb 19 '20 at 20:41
  • omg. i dont care if it was a day later lol. and yes i am still fresh to c++ so thanks. i did not find that on my own but since you did feel free to mark this as a dupe i'll be reading over that a few times to ensure i understand. many thanks! although i do want to add, at the end the last comment says to avoid using it :) – James Wilson Feb 20 '20 at 13:41
  • so thats a great leap in the right direction. but i encounter a small issue with the idea. so the 1 "app" type class needs to save that pointer to a function, but you cannot use auto. and void * doesnt work. I wont always know the full prototype for the function it needs to call. I think for now i'm going to rework my code and use something like a testable flag to see if the thing was done and act on it from there instead. thank you very much for your hope so far tho. i am going to keep looking into this for future needs. – James Wilson Feb 20 '20 at 14:09
  • yw. I'm running into a bit of an issue here... if auto won't work, because the type changes or is unknown... that's impossible. void pointers will point to literally anything. And you can cast them to literally anything, but you have to know what it is when you cast it. Other languages disallow this. I prefer C++'s allowance, but definitely avoid it as much as you'd avoid Goto. Your architecture may need a 2nd look, from someone you can tell what you're doing. Chances are it'll be easier than expected. – Stephen J Feb 20 '20 at 21:41