简体   繁体   中英

c++ calling a template functor but not passing an argument

I'm trying to understand two things in c++, functors and passing reference. Below is example code for the library libtins available here libtins example . The function snifferloop() takes the template functor doo() .

The declaration for the functor is bool doo(PDU &some_pdu) so the argument some_pdu of type PDU is passed by reference to the function.

However, doo() is called as sniffer.sniff_loop(doo); and no argument appears to be passed to doo() .

Could somebody explain what is happening here please? Apologies if it is a basic.

bool doo(PDU &some_pdu) {
    // Search for it. If there is no IP PDU in the packet, 
    // the loop goes on
    const IP &ip = some_pdu.rfind_pdu<IP>(); // non-const works as well
    std::cout << "Destination address: " << ip->dst_addr() << std::endl;
    // Just one packet please
    return false;
}

void test() {
    SnifferConfiguration config;
    config.set_promisc_mode(true);
    config.set_filter("ip src 192.168.0.100");
    Sniffer sniffer("eth0", config);
    sniffer.sniff_loop(doo);
}

sniffer.sniff_loop(doo) doesn't call doo . It calls sniffer.sniff_loop , passing a pointer to doo as argument to sniffer.sniff_loop . Then sniffer.sniff_loop will use that argument to call doo , passing the argument to doo .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM