Reputation: 163
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);
}
Upvotes: 0
Views: 89
Reputation: 409136
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
.
Upvotes: 3