Click here to Skip to main content
15,891,905 members
Articles / Programming Languages / C++
Tip/Trick

Named Objects in C++

Rate me:
Please Sign up or sign in to vote.
2.78/5 (7 votes)
30 May 2011CPOL1 min read 21.1K   5   5
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:


C++
class GlobalEventClass //this object will host our event
{

public:
  //
  // the event that we are going to use to query our object names
  //
  __event void GetObjectByName( __in const char* _cpName, __out void** _pDest );

}Parent;


class Child //any random receiver class
{
private:

  //
  // Each object has to carry its own name (can be anything)
  //
  const char* cpObjectName;

public:

  ~Child()
  {
      __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...


Thanks.

License

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


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

Comments and Discussions

 
GeneralI would prefert to use hash map . it should offer better per... Pin
franck-paris-france7-Jun-11 12:05
franck-paris-france7-Jun-11 12:05 
GeneralPlease note that Event handling support for native C++ class... Pin
George L. Jackson7-Jun-11 2:43
George L. Jackson7-Jun-11 2:43 
GeneralInteresting, my 5 pats :) Pin
Espen Harlinn30-May-11 11:12
professionalEspen Harlinn30-May-11 11:12 
Interesting, my 5 pats Smile | :)
GeneralIssues Pin
David O'Neil9-Jun-11 20:41
professionalDavid O'Neil9-Jun-11 20:41 
GeneralBut why? Pin
Alexander Voronin6-Jun-11 23:55
Alexander 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.