Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add operator definition to an existing struct from a header file?

I am developing a SNMP Agent in Windows. In the header file snmp.h there is a struct which defines the OID identifier for a value and the definition for that is as follows:

typedef struct {
  UINT   idLength;
  UINT * ids;
} AsnObjectIdentifier; 

I want to use this AsnObjectIdentifier as a key to an unordered_map but the struct definition does not overload the == operator, which brings me to the question whether if it is possible to add an operator overload to an already defined struct or would I have to just have my custom struct wrapping the AsnObjectIdentifier variable.

like image 288
Navjot Singh Avatar asked Sep 23 '19 11:09

Navjot Singh


1 Answers

Yes, you can define operators outside of the class:

bool operator==(AsnObjectIdentifier const& lhs, AsnObjectIdentifier const& rhs)
{
    return /* whatever */;
}

Alternatively you can define a custom equality function object and pass it to unordered_map's fourth template parameter.

like image 61
Pubby Avatar answered Oct 04 '22 21:10

Pubby