简体   繁体   中英

overloading operator++ for a pointer

i need to loop through a memory region which contains my own defined types:

struct ModuleList 
{
    char ModuleName[64];
    int32_t NumberOfFunctions;

};

struct FunctionList
{
    char FunctionName[64];
    DWORD FunctionAddress;
};

These lays out in memory like this:

0: ModuleList[0]
0 + sizeof(ModuleList): FunctionsList[NumberOfFunctions]
0 + sizeof(ModuleList)+ NumberOfFunctions * sizeof(FunctionList): ModuleList[1]
.
.
.

What i want to do is, overloading operator ++ for ModuleList* so i can easily increment my pointer correctly, because default ++ operator for pointer only increments for sizeof(ModuleList) and i want to increment for sizeof(ModuleList) and size of functions (which is NumberOfFunctions * sizeof(FunctionList)). These structs in memory are completely dynamic, which my program gets over the network. Currently, i am doing this:

unsigned short ModuleCount = 0;
BYTE* pTemp = (BYTE*)MemoryAdr;
for (; std::string(((ModuleList*)(pTemp))->ModuleName).find(".dll") != std::string::npos; pTemp += ModuleListSize + ((ModuleList*)(pTemp))->NumberOfFunctions * FunctionListSize)
    ModuleCount++;

But i think it looks ugly. Any suggestions?

C++ requires that your operator overloads take at least one operand of a "class type" or enumeration type. So, you can't overload operator for intrinsic/POD types and you need to wrap your struct with self-made smart-pointer class (as Neil proposed).

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