Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

class member function variable pointing to another class function member

can you please explain me why this code shows nothing except for the last std::cout line in main()? Checked through similar threads on stackoverflow.com, was not able to connect them to mine, is this pointing legal at all? I try to set a function pointer of a class to another class function:

#include <iostream>

class container;

class one {
public:
one() 
    { 
        eventsHandler = NULL; 
    };
~one() {}; 

void (container::*eventsHandler)();
};

class container {
public:
    container() 
    { 
        zone = new one;
        zone->eventsHandler = &container::events;
    };
    ~container()
    { 
        delete zone; 
    };

    one *zone;
    void events()
    {
        std::cout << "event handler is on..." << std::endl;
    };
};

int main()
{
    container *test = new container;
    test->zone->eventsHandler;

    std::cout << "just checker..." << std::endl;
    delete test;

    system("pause");
};
like image 636
Maximal Xarra Avatar asked Aug 22 '26 05:08

Maximal Xarra


1 Answers

You should use operator->* for calling the member function pointer, and assign test as the object which will be called on,

(test->*test->zone->eventsHandler)();

or more clear,

(test->*(test->zone->eventsHandler))();

LIVE

like image 88
songyuanyao Avatar answered Aug 23 '26 19:08

songyuanyao



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!