Click here to Skip to main content
13,048,805 members (81,516 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


5 bookmarked
Posted 30 May 2011

Named Objects in C++

, 30 May 2011
Rate this:
Please Sign up or sign in to vote.
SLQ lazy? Object pointers are annoying? How about you look your object up by assigning it a unique name?

Named Objects in C++

So I have been thinking about how to manage multiple objects without having to store their references/addresses in an array or map (or something even more annoying). What I wanted to do is just create an object (and assign it a name) anywhere in memory and when I need to look it up, I just use its name to find it. But how do I do that: the name of the game is "native c++ events" kaboom.

I have written a short example code below. In it, I create an event inside of some sort of master object, which is defined globally. Your can query a name by raising this event, provoking it to call the handlers in every hooked object. If the object is found, the handler will place its object address in a given pointer:

class GlobalEventClass //this object will host our event
  // the event that we are going to use to query our object names
  __event void GetObjectByName( __in const char* _cpName, __out void** _pDest );

class Child //any random receiver class
  // Each object has to carry its own name (can be anything)
  const char* cpObjectName;
      __unhook( &GlobalEventClass::GetObjectByName, Parent, &Child::cpObjectName, this );
  Child( __in const char* _cpMyName )
     : cpObjectName( _cpMyName ) // we store the pointer to our assigned name
     // now we need to hook the created instance to the event source
      __hook( &GlobalEventClass::GetObjectByName, Parent, 
              &Child::cpObjectName, this );
  // the next function is called in every object when the event (above) 'interrupts'
  void NameQueryHandler( __in const char* _cpName, __out void** _pDest )
      // excuse me my c-c++ scramble
      if( strstr( cpObjectName, _cpName ) != nullptr )
          // we now know that this object is the object that is being searched for
          // so we fill the pointer we were given in the parameter list with our address
          *_pDest = (void*) this;
}; //Child
Child* GetChildByName( __in const char* _cpObjectName )
    Child* X = nullptr;
    __raise Parent.GetObjectByName( _cpObjectName ,(void**) &X);
    return X;
int main( void ) // for example...
    Child   Who("Bobby"),Needs("Danny"), *Labels = 
                     new Child("Jamal");
    // lets find Jamal, okay?
    Child*  Jamal = GetChildByName("Jamal");
    return true;

Now I can use any object from anywhere, without having to store its address. I figured this concept would be somewhat useful when you manage different containers, user/client info for example.

The reason why I put this here is because, earlier I was searching Google for hours to find a good implementation for this, hoping there would be a trick to it. Unfortunately without any results, so I had to figure something out by myself (quick! someone pat me on the back!). Well, have fun then...



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Hubert Haien
Software Developer (Junior)
Germany Germany
Eager to show what I can do, but still haven't had the chance todo so.

You may also be interested in...

Comments and Discussions

GeneralI would prefert to use hash map . it should offer better per... Pin
franck-paris-france7-Jun-11 12:05
memberfranck-paris-france7-Jun-11 12:05 
GeneralPlease note that Event handling support for native C++ class... Pin
George L. Jackson7-Jun-11 2:43
memberGeorge L. Jackson7-Jun-11 2:43 
GeneralInteresting, my 5 pats :) Pin
Espen Harlinn30-May-11 11:12
mvpEspen Harlinn30-May-11 11:12 
GeneralIssues Pin
David O'Neil9-Jun-11 20:41
memberDavid O'Neil9-Jun-11 20:41 
GeneralBut why? Pin
Alexander Voronin6-Jun-11 23:55
memberAlexander Voronin6-Jun-11 23:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 30 May 2011
Article Copyright 2011 by Hubert Haien
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid