c++ - How to make a getter function for a double pointer? -


i needing modify open source project prevent reusing code (more efficient create getgamerulesptr() function keep going engine retrieve it. problem is, stored void **g_pgamerules. ive never grasped concept of pointer pointer, , bit confused.

i creating getgamerules() function retrieve pointer, im not sure if getter function should void* ret type , return *g_pgamerules, or how should go this. brushing on pointer usage now, wanted find out proper method learn from.

here code, lines 58-89 sdk function retrieve g_pgamerules pointer game engine. other functions adding getter function to.

// extension.cpp  class sdktools_api : public isdktools { public:     virtual const char *getinterfacename()     {         return sminterface_sdktools_name;     }      virtual unsigned int getinterfaceversion()     {         return sminterface_sdktools_version;     }      virtual iserver *getiserver()     {         return iserver;     }      virtual void *getgamerules()     {         return *g_pgamerules;     } } g_sdktools_api;  // extension.h  namespace sourcemod {     /**      * @brief sdktools api.      */     class isdktools : public sminterface     {     public:         virtual const char *getinterfacename() = 0;         virtual unsigned int getinterfaceversion() = 0;     public:         /**          * @brief returns pointer iserver if 1 found.          *          * @return          iserver pointer, or null if sdktools unable find one.          */         virtual iserver* getiserver() = 0;          /**          * @brief returns pointer gamerules if 1 found.          *          * @return          gamerules pointer, or null if sdktools unable find one.          */         virtual void* getgamerules() = 0;     }; }  // vglobals.cpp  void **g_pgamerules = null; void *g_entlist = null;   void initializevalveglobals() {     g_entlist = gamehelpers->getglobalentitylist();      char *addr;  #ifdef platform_windows     /* g_pgamerules */     if (!g_pgameconf->getmemsig("creategamerulesobject", (void **)&addr) || !addr)     {         return;     }      int offset;     if (!g_pgameconf->getoffset("g_pgamerules", &offset) || !offset)     {         return;     }     g_pgamerules = *reinterpret_cast<void ***>(addr + offset); #elif defined platform_linux || defined platform_apple     /* g_pgamerules */     if (!g_pgameconf->getmemsig("g_pgamerules", (void **)&addr) || !addr)     {         return;     }     g_pgamerules = reinterpret_cast<void **>(addr); #endif } 

you want return void*, , casting appropriate sometype** within implementation code. because void** has strange semantics (which can't find on google right now). tells more info user need. whole point of using void* begin avoid giving information user don't need.

if option, i'd recommend avoiding void* altogether, , providing opaque reference type them call apis with. 1 way define fake structure, struct gameobjectref {};, , pass user gameobjectref*, casted whatever pointer system uses. allows user write typed code, can't accidentally provide wrong pointer type functions, can void*.

how pointers (and pointers-to-pointers) work:

imagine asking me aunt lives. then, hand piece of paper address go to. piece of paper pointer house.

now, take piece of paper address, take photo of digital camera, , place image onto personal wiki site.

now, if sister calls, asking aunt's address, tell on wiki. if asks url, write on piece of paper her. second piece of paper pointer pointer house.

you can see how address isn't same real thing. because has website address doesn't mean know aunt's address. , because have aunt's address doesn't mean they're knocking on door. same true pointers objects.

you can see how can make copies of addresses (pointers), doesn't make copy of underlying object. when take photo of aunt's address, aunt doesn't shiny new house.

and can see how dereferencing pointer lead original object. if go wiki site, aunt's address. if drive address, can leave package on doorstep.

note these aren't perfect metaphors, close enough descriptive. real pointers-to-pointers lot cleaner examples. describe 2 things - type of final object (say, gameobject), , number of levels of indirection (say, gameobject** - 2 levels).


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -