Re: TRICK: Unique ID'ing



Peter Oliphant wrote:
> Sometimes it's hard to get straight when passing or storing or
> returning an instance of a class whether you are still dealing with
> the original object or a copy. For example, the '=' operator used on
> pointers to two instance of a class can be overloaded to return the
> pointer to the target instance or a pointer to a copy of the target
> instance. When passing an instance of a class it can be done so the
> method will manipulate the instance itself (reference) or a copy of
> the instance (value). Implicit copy constructing can also confuse
> things. The '==' operator can be overloaded to return true only if
> the two instances are the same instance, or it can return true when
> the two merely have the same value (e.g., two long variables when
> compared for equality will return if they have the same value, not if
> they are the same variable). So it's not always obvious if one is
> manipulating the object desired or a copy of the object.
> To combat this, I use the following technique, which could be called
> 'unique ID'ing'. Here is reduced coded to show how it works:
>
> class MyClass
> {
> public:
> MyClass( ) { m_Init() ; }
> ~MyClass( ) {}
>
> public:
> long ID( ) { return m_ID ; }
>
> private:
> void m_Init( )
> {
> m_ID = ++s_ID ;
> }
>
> private:
> long m_ID ;
> static long s_ID = 0 ;
> } ;
>
> Since 's_ID' is static it is a single variable which will be used by
> all instances of the class created. The m_Init( ) (which needs to be
> put in every constructor) will increment this static variable and
> then store it's new value in the instance's personal copy of 'm_ID'.
> Thus, 'm_ID' is unique for each instance created!
>
> Now if you have an instance of MyClass that you suspect might be a
> copy of the intended instance, just keep track of ID of the instance
> you want (via ID( )) and compare it against the suspect. If it
> matches you know they are the same object. If not, you know they
> aren't!
> There is a side bonus to this. If you examine the value of 's_ID' it
> will tell you exactly how many instances of MyClass have been created
> so far in the application run! Thus, one can better discover
> situations where copies are being created which are unintended...
>
> There might be other ways of doing this, possibly even built into the
> system, but I still think this is a cool trick... : )

Old trick.

An even easier way - just cast the this pointer to an integer type and call
that the ID. Guaranteed unique with absolutely no overhead.

-cd


.



Relevant Pages

  • TRICK: Unique IDing
    ... Sometimes it's hard to get straight when passing or storing or returning an ... pointer to a copy of the target instance. ... class MyClass ... ID) and compare it against the suspect. ...
    (microsoft.public.dotnet.languages.vc)
  • Re: [PATCH] [0/9] Use 64bit x86 machine check code for 32bit too
    ... warning: passing argument 2 of ‘strict_strtoull’ makes integer from pointer without a cast ...
    (Linux-Kernel)
  • Re: ByVal vs. ByRef
    ... But I still don't understand why I have to pass the TreeNode argument ByVal. ... I understand why passing it ByVal works. ... What is "the Value" of a reference variable? ... A "pointer" to the object. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Can a ref parameter be saved for later use?
    ... but in this scenario the 'passing back' will have to happen later. ... Form(ref string[] strLst) { ... Even in C/C++ it's a dangerous thing to capture a pointer to some data and save it for reuse later. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: why no copy constructor in java?
    ... Here only the pointer value is copied, both pointers will then point to ... MyClass Obj_1 = MyClass; ... default memberwise shallow copy then Obj_1 and Obj_2 will both have ... > behaviour(shallow copying) is usually un-acceptable. ...
    (comp.lang.java.programmer)

Loading