0

I came across the following code in a project.

map_erase_if(cfgIp.m_raw, RawCreate());  

I understand that this will call the function defined by () inside the structure.

The Rawcreate is a structure which is given below .

 struct RawCreate {  
         bool operator()(const Device::StoreElm& el) {  
             Pcap* pcap = NULL;  
             pcap = Pcap::findServer(el.second->name());  
             if (!pcap) {  
                 try {  
                     MEM_NEW(pcap,Pcap(*el.second));  
                 } catch (Exception& ex) {  
                     MAND_LOG(DBG_SIPTCP, "Error:%s", ex.what());  
                 }  
             }  
             for (RejItr itr = CfgIp::m_rejectList.begin(); itr !=   CfgIp::m_rejectList.end(); ++itr) {
                             if(ip_equal(itr->first, pcap->getInterface(),   false) && !itr->second.empty()) {  
                                 pcap->blockReg();  
                              }  
                          }  
             if (pcap) {  
                 MEM_DELETE(el.second);  
                 return true;  
             }  
             return false;  
         }  
     };  

I have seen operator overloading with class objects.I know class and structure has only few differences in cpp.But using overloading within a structure in this way is a new thing for me.Can any one clarify its usage?

achoora
  • 1,270
  • 1
  • 16
  • 32
  • this post will help you to understand : http://stackoverflow.com/questions/92859/what-are-the-differences-between-struct-and-class-in-c, so no difference except default access-specifier – Garf365 Feb 18 '16 at 09:45
  • http://en.cppreference.com/w/cpp/language/class Basically, `struct` and `class` denote the same entity. – user3159253 Feb 18 '16 at 09:46
  • 7
    `RawCreate()` doesn't call `operator()`. It just creates an instance of that type. It's possible that `operator()` will be called from inside the `map_erase_if` function which you haven't shown. – interjay Feb 18 '16 at 09:46
  • In the map_erase_if the RawCreate() is used as a predicate for std::find_if – achoora Feb 18 '16 at 09:52

3 Answers3

4
RawCreate()

I understand that this will call the function defined by () inside the structure.

If RawCreate is a function, then this will call it. If it is a type, then it will construct an instance. RawCreate is a class, so the latter is true. T::operator() is not called by this expression.. unless RawCreate is a variable of type T that defines the operator.

I know class and structure has only few differences in cpp

Just to clarify, the only difference is that classes declared with the struct keyword and those declared with class keyword have a different access specifier by default.

Can any one clarify its usage?

The documentation of map_erase_if should describe how the operator is used. Presumably, the operator() of the functor is used as a predicate for erase.

eerorika
  • 232,697
  • 12
  • 197
  • 326
1

RawCreate() creates an instance of RawCreate.

Your code works like this code:

RawCreate c;
map_erase_if(cfgIp.m_raw, c);

but avoids creating an unnecessary variable.

Presumably, map_erase_if is then going to call the instance's operator() with each member of cfgIp.m_raw (or pass the instance on to something that does).

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
0

As the others said, RawCreate() does not call the operator (), it just creates an instance (which is then copied over).

The operator () is then (most likely) called for each item of the map when iterating during map_erase_if() (and if the operator returns true, the item will be erased, if the function works as the name advertises).

EmDroid
  • 5,918
  • 18
  • 18